Page 1 of 1

help with regular expressions, please.

Posted: Tue Aug 10, 2010 10:31 pm
by elietphpcoder
Hi all,

I am trying to write a simple regular expressions that searches for a custom start tag and end tag, the search needs to go through multiple lines to find the end tag after finding the beginning tag.

The beginning tag: //[(
The ending tag: //)]

Here is what I have but it only works in the begging and ending tag occur on the same line. I need it to continue through each line until it finds the end tag to make a match.

preg_split("/\/\/\[\((.*)\/\/\)\]/", $output, -1, PREG_SPLIT_DELIM_CAPTURE);

Thank you for your help in advance.

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 2:55 am
by novito
Hi there,

Is this what you are trying to do?

$iniTAG = "\/\/\[\(";
$endTAG = "\/\/\)\]";
$middle = ".*";
$testTHIS = "Hi there/n this is a new line /n//[(Looking for a match/n/n//)]tururur /n";
$found2 = preg_match("/".$iniTAG.$middle.$endTAG."/",$testTHIS);

I hope is this :]

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 4:19 am
by elietphpcoder
That works only if the entire code is all in a single line, if you add line breaks in it, it will no longer work.

Example:

$testTHIS = "Hi there/n this is a new line /n
//[(
Looking for a match/n/n
//)]

tururur /n";

This is the problem im having getting it to read multiple lines between the start tag and end tag.

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 4:34 am
by elietphpcoder
I discovered the answer on my own, thank you for your help though. here is the answer that works:

$middle = "([^<]*?)";

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 6:16 am
by novito
Hi there,

I was assuming that a break line is done by '/n'.

In any case, I'm glad you found the solution :)

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 6:18 am
by novito
Could you clarify what this does ("([^<]*?)").

Thanks :)

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 8:03 am
by shawngoldw
This is not a solution, but just a sugestion. If you use a different character than / to enclose the regex, it will be much cleaner because you won't havae to escape all those slashes. I like to use ! because I hardly ever look for it.

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 9:06 am
by novito
That's a good suggestion!

I didn't know you could change the enclosers @_@

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 9:20 am
by PradeepKr
Firstly,
I think ? is not needed here ("([^<]*?)")
("([^<]*)") should work.

Secondly,
This cannot be used in your case since your end tag is multi-character one. [^<]* can match everything till the very first < (excluding it). But in your case < (single char) has to substituted with four chars //)]
You can use non-greedy match (.*?)

Thirdly,
You should use "s" modifier to handle newline character as a normal character(of-course take whole content into on string first).


Try this(note: I have used ! and "s"),

Code: Select all

if(preg_match("!//\[\((.*?)//\)\]!s", $output, $matches) )
{
  echo $matches[1];
}

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 10:06 am
by novito
What is the purpose of doing (.*)? instead of (.*) ??

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 12:32 pm
by novito
Hi there,

I have some doubts on reg expression:

If I want to accept those words that they have an a followed by 2 to 4 Ts, I would do:

$string = "attttttt";
echo preg_match("/at{2,4}/", $string);

However, this echo returns me 1, which means that it has found one match. Isn't the {2,4} telling the function that I want exactly 2 to 4 Ts appearances?

Thanks :)

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 12:41 pm
by shawngoldw
Yes but it does not say that there can not be anything after the 2 to 4 t's. It will work with attttfafdadskjdasljhgnljfnnsda too or ffffattttzzz

You want
/^at{2,4}$/

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 12:45 pm
by AbraCadaver
novito wrote:Hi there,

I have some doubts on reg expression:

If I want to accept those words that they have an a followed by 2 to 4 Ts, I would do:

$string = "attttttt";
echo preg_match("/at{2,4}/", $string);

However, this echo returns me 1, which means that it has found one match. Isn't the {2,4} telling the function that I want exactly 2 to 4 Ts appearances?

Thanks :)
Yes, you have an a followed by 4 t's, there just so happens to be 3 additional ones. You would need to tell it that after 2-4 t's there should not be a t:

Code: Select all

echo preg_match("/at{2,4}[^t]/", $string);

Re: help with regular expressions, please.

Posted: Wed Aug 11, 2010 4:14 pm
by novito
Thanks for the explanation :-).