tags:

views:

73

answers:

2

I have the following regular expression:

SOMETHING(.*?),(1|0)\\);

It needs to match SOMETHING then anything, then a comma, then 1 or 0 followed by ); but for some reason the last bracket isn't being matched, an example string:

SOMETHINGdfsdfdsfd dsffdsdf dfsfds FEHEF777a ,0);

the bold part is the ending. Am I escaping the ) wrong? \ should work...

example php where $o is my string

preg_match_all('%INSERT INTO (.*?),(1|0)\);%sm', $o, $matches);
+1  A: 

Your original post doesn't show you are escaping it. You need:

SOMETHING(.*?),(1|0)\);

If you have that, what is the language running the regular expression, e.g. Java, PHP, Perl?

UPDATE

There are some things to consider. You are using a non-greedy expression in your first capture. In combination with your modifiers (multiline and dot all) this could be conflicting. Furthermore, based on your input you may need to escape the first set of parenthesis.

Regular Expressions are very powerful, but only as powerful as their creators. That is to say they often fail unless you know exactly what you are wanting to match.

Jason McCreary
seems markdown broke it, a user fixed my post. yes, i am escaping the bracket.
Yes, 't was hidden in the source ;)
Wrikken
+1 - this works for me, the only thing I would do different is change the "1|0" to a "[10]".
Matthew J Morrison
Out of curiosity, what makes it non-greedy?
George Marian
`.*?` is a non-greedy match of *anything*. Specifically the `?` suffix. Same for `.+?`
Jason McCreary
OIC, thanks. (As it turns out, it would've been a quick search to confirm my assumption.)
George Marian
+1  A: 

It does work here, did you maybe mean to get the ending ); in there? If so:

 SOMETHING(.*?),((?:1|0)\);)

Otherwise: what language are we talking about? Do you need to double or triple the \? (\\ / \\\ can be possible in some implementations).

Edit: OK, php

$php -r 'var_dump(preg_match_all("/SOMETHING(.*?),(1|0)\);/","SOMETHINGdfsdfdsfd dsffdsdf dfsfds FEHEF777a ,0);",$matches),$matches);'
int(1)
array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(49) "SOMETHINGdfsdfdsfd dsffdsdf dfsfds FEHEF777a ,0);"
  }
  [1]=>
  array(1) {
        [0]=>
    string(36) "dfsdfdsfd dsffdsdf dfsfds FEHEF777a "
  }
  [2]=>
  array(1) {
    [0]=>
    string(1) "0"
  }
}

Perfect match.

Wrikken
Keep in mind, you removed the user's modification flags. Which I believe is the problem ;)
Jason McCreary
Nah, tested those, just not in output. Added newlines and extra commas and all in combination with /sm. Splurged with `0)` at other positions not followed by `;`, Still matches.
Wrikken
And I did not remove them, failed to add them when finally the language became available :)
Wrikken
@Wrikken Sounds good.
Jason McCreary