regexp problem, a little strange

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
avastreg
Forum Newbie
Posts: 3
Joined: Tue Aug 12, 2008 11:45 am

regexp problem, a little strange

Post by avastreg »

Hi to all,

i need to solve a problem with a regular expression but so far i don't get a solution for that.

I need to obtain, in a string, all the occurrences between ' ' , but ignoring something like \' in the middle.

Practical example:

Code: Select all

 
$str = "this is a ' test for my regexp. i have 
something called \'Something\', but i don't want 
to check that quotes'. so 'on' etc..";
 
So, in this case, i want to ignore that \' .. my result array must be

Code: Select all

0 => 'test for my regexp. i have something called \'Something\', but i don't want 
to check that quotes
1 => 'on'
covering multiline too.

i don't want to get

Code: Select all

0 => 'test for my regexp. i have something called \'
, and this is what i get with my current reg exp.

My (best) current reg exp to do that :

Code: Select all

preg_match_all("/'[^']+'/s", .., ...);
I have tried with assertion too, but i made some mistake and it doesn't go well :banghead:

Any help would be appreciated!!

Alessandro
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regexp problem, a little strange

Post by prometheuzz »

In your example, you didn't escape the apostrophe in the sub string "don't". I assume that was a typo.

You could try something like this:

Code: Select all

if(preg_match_all("/'.*?(?<!\\\\)'/s", $str, $matches)) {
  print_r($matches);
}
avastreg
Forum Newbie
Posts: 3
Joined: Tue Aug 12, 2008 11:45 am

Re: regexp problem, a little strange

Post by avastreg »

prometheuzz wrote:In your example, you didn't escape the apostrophe in the sub string "don't". I assume that was a typo.

You could try something like this:

Code: Select all

if(preg_match_all("/'.*?(?<!\\\\)'/s", $str, $matches)) {
  print_r($matches);
}
thanks you're right "don't" was my typo "mistake". Assume "dont" :P

Tomorrow at work i'll try again, hope to solve with your solution! :D
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regexp problem, a little strange

Post by prometheuzz »

avastreg wrote:...

thanks you're right "don't" was my typo "mistake". Assume "dont" :P

Tomorrow at work i'll try again, hope to solve with your solution! :D
You're welcome and I'm sure it will work. This was the output of your example String:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => ' test for my regexp. i have 
something called \'Something\', but i don\'t want 
to check that quotes'
            [1] => 'on'
        )
 
)
(note that the forum software "eats up" the \ in front of the apostrophes)
avastreg
Forum Newbie
Posts: 3
Joined: Tue Aug 12, 2008 11:45 am

Re: regexp problem, a little strange

Post by avastreg »

thank you, it work perfectly and solved my problem, as you said. Thanks for welcome me too :mrgreen:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: regexp problem, a little strange

Post by prometheuzz »

avastreg wrote:thank you, it work perfectly and solved my problem, as you said. Thanks for welcome me too :mrgreen:
Good to hear it and you're welcome.
Post Reply