help with regular expressions, please.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elietphpcoder
Forum Newbie
Posts: 5
Joined: Tue Aug 10, 2010 10:27 pm

help with regular expressions, please.

Post 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.
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: help with regular expressions, please.

Post 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 :]
elietphpcoder
Forum Newbie
Posts: 5
Joined: Tue Aug 10, 2010 10:27 pm

Re: help with regular expressions, please.

Post 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.
elietphpcoder
Forum Newbie
Posts: 5
Joined: Tue Aug 10, 2010 10:27 pm

Re: help with regular expressions, please.

Post by elietphpcoder »

I discovered the answer on my own, thank you for your help though. here is the answer that works:

$middle = "([^<]*?)";
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: help with regular expressions, please.

Post 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 :)
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: help with regular expressions, please.

Post by novito »

Could you clarify what this does ("([^<]*?)").

Thanks :)
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: help with regular expressions, please.

Post 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.
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: help with regular expressions, please.

Post by novito »

That's a good suggestion!

I didn't know you could change the enclosers @_@
PradeepKr
Forum Newbie
Posts: 14
Joined: Wed Aug 11, 2010 8:29 am

Re: help with regular expressions, please.

Post 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];
}
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: help with regular expressions, please.

Post by novito »

What is the purpose of doing (.*)? instead of (.*) ??
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: help with regular expressions, please.

Post 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 :)
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: help with regular expressions, please.

Post 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}$/
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: help with regular expressions, please.

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
novito
Forum Commoner
Posts: 26
Joined: Mon Apr 07, 2008 11:08 am

Re: help with regular expressions, please.

Post by novito »

Thanks for the explanation :-).
Post Reply