Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 19, 2007 6:29 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Feb 19, 2007 7:40 am
What have you tried?
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 19, 2007 7:47 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Feb 19, 2007 7:56 am
Experiment.
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 19, 2007 8:00 am
I donno where to start Please show me some ways
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Feb 19, 2007 8:09 am
Make a pattern that can capture one, then another that captures the second. Combine them.
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 19, 2007 8:12 am
Im a very newbie in regex i've no experience in it
Just now i've tried
But it didn't worked
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 19, 2007 8:32 am
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)
Last edited by
nwp on Mon Feb 19, 2007 8:35 am, edited 1 time in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Feb 19, 2007 8:35 am
What are all the pluses for? I only see a need for two.
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 19, 2007 8:39 am
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 ??
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Mon Feb 19, 2007 8:43 am
You know what pluses do in regex, right?
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 19, 2007 8:45 am
I can decrease 1 "+"
but how only 2 ??
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Mon Feb 19, 2007 10:48 am
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"
GeertDD
Forum Contributor
Posts: 274 Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium
Post
by GeertDD » Mon Feb 19, 2007 2:32 pm
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!
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 19, 2007 10:00 pm
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____
)
Last edited by
nwp on Mon Feb 19, 2007 10:06 pm, edited 1 time in total.