Reputation: 3409
I have created a Regular Expression (using php) below; which must match ALL terms
within the given string that contains only a-z0-9
, .
, _
and -
.
My expression is: '~(?:\(|\s{0,},\s{0,})([a-z0-9._-]+)(?:\s{0,},\s{0,}|\))$~i'
.
My target string is: ('word', word.2, a_word, another-word)
.
Expected terms
in the results are: word.2
, a_word
, another-word
.
I am currently getting: another-word
.
My Goal
I am detecting a MySQL function from my target string, this works fine. I then want all of the fields from within that target string. It's for my own ORM.
I suppose there could be a situation where by further parenthesis are included inside this expression.
Upvotes: 0
Views: 96
Reputation: 571
From what I can tell, you have a list of comma-separated terms and wish to find only the ones which satisfy [a-z0-9._\-]+
. If so, this should be correct (it returns the correct results for your example at least):
'~(?<=[,(])\\s*([a-z0-9._-]+)\\s*(?=[,)])~i'
The main issues were:
(?<=...
) and a lookahead ((?=...
)EDIT: Since you said in a comment that some of the terms may be strings that contain commas you will first want to run your input through this:
$input = preg_replace('~(\'([^\']+|(?<=\\\\)\')+\'|"([^"]+|(?<=\\\\)")+")~', '"STRING"', $input);
which should replace all strings with '"STRING"', which will work fine for matching the other regex.
Upvotes: 1
Reputation: 3240
Maybe using of regex is overkill. In this kind of text you can just remove parenthesis and explode string by comma.
Upvotes: 1