1
RicoCali
Need help from Regular Expression Guru
  • 2004/1/13 4:37

  • RicoCali

  • Not too shy to talk

  • Posts: 120

  • Since: 2002/7/29


I have an example below:

foobar1
foobar2
foobar3
foobar4

How do I do I use regular expression that finds a match on foobar2 preceding foobar3?

I tried:

^foobar2$^foobar3$

..but that doesn't work. I've checked all the forums and I can't seem to find a solution for this

2
Dave_L
Re: Need help from Regular Expression Guru
  • 2004/1/13 4:59

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


<?php

$text 
"
foobar1
foobar2
foobar3
foobar4
"
;

if (
preg_match('/foobar2s*foobar3/'$text)) {
   echo 
"Matchedn";
} else {
   echo 
"Not matchedn";
}

?>

3
RicoCali
Re: Need help from Regular Expression Guru
  • 2004/1/13 5:08

  • RicoCali

  • Not too shy to talk

  • Posts: 120

  • Since: 2002/7/29


Thank you Dave...That worked perfect! But I got another question... I also tried this:

if (preg_match('/foobar2\s*foobar4/', $text)){
echo "Matched\n";
}else{
echo "Not matched\n";
}


...to match on

foobar2
foobar3
foobar4


...but this syntax was the only thing that seems to work:

if (preg_match('/foobar2\s*foobar3\s*foobar4/', $text)) {
echo "Matched\n";
} else {
echo "Not matched\n";
}


...is there a condense way to do this?

4
skalpa
Re: Need help from Regular Expression Guru
  • 2004/1/13 11:06

  • skalpa

  • Quite a regular

  • Posts: 300

  • Since: 2003/4/16


The \s* part of Dave's exp means "one or more spaces", so in this case it doesn't work because you have something else than spaces between foobar2 and foobar4...

/foobar2.*foobar4/ (fb2 and fb4 with one or more chars in between, whether there are spaces or not )

Skalpa.>

5
Dave_L
Re: Need help from Regular Expression Guru
  • 2004/1/13 14:11

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


skalpa's regex pattern is correct, but actually * means "zero or more". + means "one or more". And note that \s matches any whitespace character: space, tab, \n, \r, etc.

6
RicoCali
Re: Need help from Regular Expression Guru
  • 2004/1/13 15:49

  • RicoCali

  • Not too shy to talk

  • Posts: 120

  • Since: 2002/7/29


Skalpa's regex didn't work. My concern is how do you wildcard muliple linebreaks as a match. With Dave's method it appears that you have to explicitly specify this (\s*) for "each" linebreak to get your result. Which is OK with me. I just want to know if there is a way to do this.

Can this be simplified?:

^.+\s*.+\s*.+\s*.+\s*.+\s*.+\s*.+\s*.+\s*.+\s*.+\s*

7
Dave_L
Re: Need help from Regular Expression Guru
  • 2004/1/13 15:58

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Using multiple * or + metacharacters gets tricky.

You can also match linebreak characters more specifically:

/foobar1\nfoobar2/

You might need to do this, to allow for different platforms:

/foobar1(\r\n|\n|\r)foobar2/

Could explain what overall problem you're trying to solve? There might be a better approach.



8
RicoCali
Re: Need help from Regular Expression Guru
  • 2004/1/13 19:58

  • RicoCali

  • Not too shy to talk

  • Posts: 120

  • Since: 2002/7/29


Well basically (without getting into to too much detail) I have a process that hits certain sites for information. The problems is this phrase can be in more than one line...sometimes three. I thought about just stipping all the linefeeds once I read the page into a buffer. But I don't want to do that if i don't have to. It seems to me that for the regex to work that you (Dave) have specified, I have to explicitly specify "\s*" for each line feed. Again with this example:

foobar1 foobar2
foobar3 foobar4
foobar5 foobar6
foobar7 foobar8
foobar9 foobar10


...I wish i can do something like this:

^foobar3.+foobar6$...where ".+" accounts for linebreaks as well. This would be more prefered to me because i don't know how many lines this phrase will take. Unfortuneately the Regular Expression I presented doesn't behave the way I want it to.

9
Dave_L
Re: Need help from Regular Expression Guru
  • 2004/1/13 23:59

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Oh, I forgot. You need the "s" modifier if you want "." to match newlines:

preg_match('/^foobar3.+foobar6$/s', ...)

Are you sure you want the "^" and "$" anchors? This pattern wouldn't match the text you posted above, since it starts with foobar1, not foobar3; and ends with foobar10, not foobar6.

10
RicoCali
Re: Need help from Regular Expression Guru
  • 2004/1/14 1:04

  • RicoCali

  • Not too shy to talk

  • Posts: 120

  • Since: 2002/7/29


Thanks Dave...that really got me into looking at modifiers...I tried (?s)foobar3.+foobar6 also and that worked. Thanks again.

Login

Who's Online

184 user(s) are online (106 user(s) are browsing Support Forums)


Members: 0


Guests: 184


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits