Page 1 of 2
^text1_(text2) extract text1 & text2
Posted: Mon Feb 19, 2007 6:29 am
by nwp
Hello
I Need some help
I have to Extract text1 and text2 from
And then i've to make $main = text1 and $sub = text2
Would anybody please make a regex
Posted: Mon Feb 19, 2007 7:40 am
by feyd
What have you tried?
Posted: Mon Feb 19, 2007 7:47 am
by nwp
I didn't tried anything cause i donno how to extract 2 strings
i've read the regex tutorial here but all of the examples were about extracting numbers and 1 numbers not 2
thanks
Posted: Mon Feb 19, 2007 7:56 am
by feyd
Experiment.
Posted: Mon Feb 19, 2007 8:00 am
by nwp
I donno where to start Please show me some ways
Posted: Mon Feb 19, 2007 8:09 am
by feyd
Make a pattern that can capture one, then another that captures the second. Combine them.
Posted: Mon Feb 19, 2007 8:12 am
by nwp
Im a very newbie in regex i've no experience in it
Just now i've tried
But it didn't worked
Posted: Mon Feb 19, 2007 8:32 am
by nwp
I think i've got it by myshelf
is almost working
but this code
Code: Select all
<?php
$string = "^name_(hello)";
header("Content-Type: text/plain");
preg_match("/^\^+(\w+)+_+(\(+\w+\))/", $string, $matches);
print_r($matches);
?>
is making output
Array(
[0] => ^name_(hello)
[1] => name
[2] => (hello)
)
But I Need
Array(
[0] => ^name_(hello)
[1] => name
[2] => hello
)
I.E. hello not (hello)
Posted: Mon Feb 19, 2007 8:35 am
by feyd
What are all the pluses for? I only see a need for two.
Posted: Mon Feb 19, 2007 8:39 am
by nwp
Ya I got it
Its working
Its The first Successful regex I made
feyd wrote:What are all the pluses for? I only see a need for two.
How ??
Posted: Mon Feb 19, 2007 8:43 am
by superdezign
You know what pluses do in regex, right?
Posted: Mon Feb 19, 2007 8:45 am
by nwp
I can decrease 1 "+"
but how only 2 ??
Posted: Mon Feb 19, 2007 10:48 am
by Kieran Huggins
Well done nwp - that was quick!
You'll be using regular expressions to make your shopping list in no time - people will think you're nuts within a week!
The + symbol means "match one or more of the previous expression"
Posted: Mon Feb 19, 2007 2:32 pm
by GeertDD
nwp wrote:Ya I got it
Its working
Its The first Successful regex I made
Be careful. Your regex may match the text you want
in this case, but that doesn't mean it is 'futureproof'. Try matching the string below. Analyze the matches and try to improve your regex.
^^^^_text1_text2__((text3____)))))
And yes, that horrible string
will match!

Posted: Mon Feb 19, 2007 10:00 pm
by nwp
Kieran Huggins wrote:The + symbol means "match one or more of the previous expression"
But What does the + symbol in ( ) means and what does the + symbol outside of th ( ) means ??
----------------------------------------------EDIT------------------------------------------------------
GeertDD wrote:^^^^_text1_text2__((text3____)))))
Amazing text1, text2, text3 are also getting extracted
Array
(
[0] => ^^^^_text1_text2__((text3____)
[1] => _text1_text2_
[2] => text3____
)