help with regular expressions, please.
Moderator: General Moderators
-
elietphpcoder
- Forum Newbie
- Posts: 5
- Joined: Tue Aug 10, 2010 10:27 pm
help with regular expressions, please.
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.
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.
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 :]
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.
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.
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.
I discovered the answer on my own, thank you for your help though. here is the answer that works:
$middle = "([^<]*?)";
$middle = "([^<]*?)";
Re: help with regular expressions, please.
Hi there,
I was assuming that a break line is done by '/n'.
In any case, I'm glad you found the solution
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.
Could you clarify what this does ("([^<]*?)").
Thanks
Thanks
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: help with regular expressions, please.
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.
That's a good suggestion!
I didn't know you could change the enclosers @_@
I didn't know you could change the enclosers @_@
Re: help with regular expressions, please.
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"),
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.
What is the purpose of doing (.*)? instead of (.*) ??
Re: help with regular expressions, please.
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
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.
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}$/
You want
/^at{2,4}$/
- 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.
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: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
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.
Re: help with regular expressions, please.
Thanks for the explanation
.