Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Why does this work but this doesn't

Why does this work but this doesn't

Scheduled Pinned Locked Moved Regular Expressions
regexhelpquestion
5 Posts 4 Posters 16 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrew St Hilaire
    wrote on last edited by
    #1

    This works without the $ but I need it to work from the end of the line not the start of the line. Working:

    (\b[A-Za-z]{1}[A-Za-z]+\b)

    Not Working:

    (\b[A-Za-z]{1}[A-Za-z]+\b)$

    What I'm trying to do is grab the street type from an address which works fine using just

    [a-zA-z]+$

    When the address is like "123 Easy Street" but it doesn't work well when there's a street direction afterwards like "123 Easy Street N". I'm trying to skip over the single character direction indicator and match "Street" whether the single character is there or not, but it's also not always "Street" as it could be avenue, crescent, drive, highway, road, etc. I think my line above will do what I need but I can't get it to work with the $ to read backwards. Any help would be appreciated!

    K Richard DeemingR J 3 Replies Last reply
    0
    • A Andrew St Hilaire

      This works without the $ but I need it to work from the end of the line not the start of the line. Working:

      (\b[A-Za-z]{1}[A-Za-z]+\b)

      Not Working:

      (\b[A-Za-z]{1}[A-Za-z]+\b)$

      What I'm trying to do is grab the street type from an address which works fine using just

      [a-zA-z]+$

      When the address is like "123 Easy Street" but it doesn't work well when there's a street direction afterwards like "123 Easy Street N". I'm trying to skip over the single character direction indicator and match "Street" whether the single character is there or not, but it's also not always "Street" as it could be avenue, crescent, drive, highway, road, etc. I think my line above will do what I need but I can't get it to work with the $ to read backwards. Any help would be appreciated!

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      Couple of questions. What regex flavor are you using POSIX, PCRE, .NET, Java, etc? Is this part of a compiled/interpreted program (e.g C++, C#, python), or is it part of something more like a shell script?

      Keep Calm and Carry On

      A 1 Reply Last reply
      0
      • K k5054

        Couple of questions. What regex flavor are you using POSIX, PCRE, .NET, Java, etc? Is this part of a compiled/interpreted program (e.g C++, C#, python), or is it part of something more like a shell script?

        Keep Calm and Carry On

        A Offline
        A Offline
        Andrew St Hilaire
        wrote on last edited by
        #3

        I'm trying to use it within Pabbly Connect, which is an integration system like Zapier or Make. I can use the following spreadsheet type formulas found here https://www.pabbly.com/spreadsheet-formulas/ and similar usually to Google sheet formulas.

        1 Reply Last reply
        0
        • A Andrew St Hilaire

          This works without the $ but I need it to work from the end of the line not the start of the line. Working:

          (\b[A-Za-z]{1}[A-Za-z]+\b)

          Not Working:

          (\b[A-Za-z]{1}[A-Za-z]+\b)$

          What I'm trying to do is grab the street type from an address which works fine using just

          [a-zA-z]+$

          When the address is like "123 Easy Street" but it doesn't work well when there's a street direction afterwards like "123 Easy Street N". I'm trying to skip over the single character direction indicator and match "Street" whether the single character is there or not, but it's also not always "Street" as it could be avenue, crescent, drive, highway, road, etc. I think my line above will do what I need but I can't get it to work with the $ to read backwards. Any help would be appreciated!

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Andrew St. Hilaire wrote:

          it doesn't work well when there's a street direction afterwards like "123 Easy Street N"

          Your pattern explicitly requires at least two letters at the end of the line, with a non-word character before them. "123 Easy Street N" only has one letter at the end of the line, and is therefore not a match for your pattern. NB: Your pattern could be simplified to:

          (\b[A-Za-z]{2,})$

          You need to consider the data you are trying to match, and come up with a pattern to match it. Given your example, you could try:

          (\b[A-Za-z]{2,}\b)(\s+[A-Za-z])?$

          which would match "Street" in both "123 Easy Street" and "123 Easy Street N".


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • A Andrew St Hilaire

            This works without the $ but I need it to work from the end of the line not the start of the line. Working:

            (\b[A-Za-z]{1}[A-Za-z]+\b)

            Not Working:

            (\b[A-Za-z]{1}[A-Za-z]+\b)$

            What I'm trying to do is grab the street type from an address which works fine using just

            [a-zA-z]+$

            When the address is like "123 Easy Street" but it doesn't work well when there's a street direction afterwards like "123 Easy Street N". I'm trying to skip over the single character direction indicator and match "Street" whether the single character is there or not, but it's also not always "Street" as it could be avenue, crescent, drive, highway, road, etc. I think my line above will do what I need but I can't get it to work with the $ to read backwards. Any help would be appreciated!

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Andrew St. Hilaire wrote:

            I'm trying to skip over the single character direction indicator and match "Street" whether the single character is there or not,

            So match the following so it returns 'Main' in the match for any of the following 1. 123 Main 2. 123 Main View 3. 123 Main N 4. 123 Main View S

            (\b[A-Za-z]+)((\s+[A-Za-z])?)$

            First clause matches the street. Second clause optionally matches a single last character. Why the extra parens in the second clause? Because I prefer to always have a match for optionals. So in this case first match is street name and second match (always there) is either something like 'S' or it is empty/null. If the parens were not there then there might or might not be a second match (you would need to test for it.)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups