Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to extract Address from js code

Tags:

c#

.net

regex

I'm trying to extract an address from js code.

I have the following line extracted from raw javascript.

(new GLatLng(45.512242,-73.554009),'2860 Main Street S',icon_near)

I've made this Regex

"\(new\sGLatLng\(           #Start of the match
(?<Lat>\-?\d{1,3}.\d+)      #Latitude
,
(?<Long>\-?\d{1,3}.\d+)\)   #Longitude   
,'
(?<Address>.+)              #The address
(?=',icon_near\))           #Positive LookAhead. The regex end just before ',icon_near)"

The problem is that I have multiple addresses together, and the regex match don't stop until the last ,icon_near\) is found.


I resolved my issue with this regex:

,
'(?<Address>.+?)'
(?=,icon_near\))
like image 486
Justin Lessard Avatar asked Feb 19 '26 08:02

Justin Lessard


1 Answers

Try this instead:

\(new\sGLatLng\(          #Start of the match
(?<Lat>\-?\d{1,3}.\d+)    #Latitude
,
(?<Long>\-?\d{1,3}.\d+)\) #Longitude   
,
'(?<Address>.+)'          #The address
(?=,icon_near\))          #Positive LookAhead. The regex end just before ',icon_near)

The single quotes were misplaced. Moreover, I remove one non needed single quote.

Demo

http://regex101.com/r/sX8dB2

like image 169
Stephan Avatar answered Feb 21 '26 21:02

Stephan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!