Here are some pointers for writing regex:
If you have a single possibility for a character, for example, the a
in array, you can indeed write it as [a]
; however, you can also write it as just a
.
If you are looking to match exactly one of something, you can indeed write it as a{1}
, however, you can also write it as just a
.
Applying this lots, your example of ^[a]{1}[r]{2}[a]{1}[y]{1}[\(]{1}[1-9\.\,\+]{1*}[\)]{1}^
reduces to ^array\([1-9\.\,\+]{1*}\)^
- that's certainly an improvement!
Next, numbers may also include 0's, as well as 1-9. In fact, \d
- any digit - is usually used instead of 1-9
.
You are using ^
as the delimiter - usually that is /
; I didn't recognize it at first. I'm not sure what you can use for the delimiter, so, just in case, I'll change it to the usual /
.This makes the above regex /array\([\d\.\,\+]{1*}\)/
.
To match one or more of a character or character set, use +
, rather than {1*}
. This makes your query /array\([\d\.\,\+]+\)/
Then, to collect the resulting numbers (assuming you want only the part between the brackets, put it in (non-escaped) brackets, thus: /array\(([\d\.\,\+]+)\)/
- you would then need to split them, first by +
, then by ,
. Alternatively, if there are exactly two lat,lng pairs, you might want: /array\(([\d\.]+),([\d\.]+)\+([\d\.]+),([\d\.]+)\)/
- this will return 4 values, one for each number; the additional stuff (+
, ,
) will already be removed, because it is not in (unescaped) brackets ()
.
Edit: If you want negative lats and longs (and why wouldn't you?) you will need \-?
(a "literal -", rather than part of a range) in the appropriate places; the ?
makes it optional (i.e. 0 or 1 dashes). For example, /array\((\-?[\d\.]+),(\-?[\d\.]+)\+(\-?[\d\.]+),(\-?[\d\.]+)\)/
You might also want to check out http://regexpal.com - you can put in a regex and a set of strings, and it will highlight what matches/doesn't match. You will need to exclude the delimiter /
or ^
.
Note that this is a little fast and loose; it would also match array(5,0+0,1...........)
. You can nail it down a little more, for example, by using (\-?\d*\.\d+)\)
instead of (\-?[\d\.]+)\)
for the numbers; that will match (0 or 1 literal -
) followed by (0 or more digits) followed by (exactly one literal dot) followed by (1 or more digits).