Ash
Ash

Reputation: 3409

Regular expression doesn't quite work

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

Answers (2)

Braiba
Braiba

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:

  • $ at the end, which was anchoring the query to the end of the string
  • When matching all you continue from the end of the previous match - this means that if you match a comma/close parenthesis at the end of one match it's not there at match at the beginning of the next one. I've solved this with a lookbehind ((?<=...) and a lookahead ((?=...)
  • Your backslashes need to be double escaped since the first one may be stripped by PHP when parsing the string.

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

Codium
Codium

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

Related Questions