Search code examples
regexalgorithmregular-language

Regular Expression To Extract Names


I have strings in this form:

"""00.000000 00.000000; X-XX000-0000-0; France; Paris; Street 12a;   
00.000000 00.000000; X-XX000-0000-0; Spain; Barcelona; Street 123;"""

I want to get specific data towns above string. How do I get this data??


Solution

  • If you just want to get the city for your given example, you could use a positive lookahead:

    \b[^;]+(?=;[^;]+;$)

    Explanation

    \b        # Word boundary
    [^;]+     # Match NOT ; one or more times
    (?=       # Positive lookahead that asserts what follows is
       ;      # Match semicolon
       [^;]+  # Match NOT ; one or more times  
       ;      # Match ;
       $      # Match end of the string
    )         # Close lookahead