^text1_(text2) extract text1 & text2

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

^text1_(text2) extract text1 & text2

Post by nwp »

Hello
I Need some help
I have to Extract text1 and text2 from

Code: Select all

^text1_(text2)
And then i've to make $main = text1 and $sub = text2
Would anybody please make a regex
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What have you tried?
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Experiment.
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

I donno where to start Please show me some ways
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 »

Im a very newbie in regex i've no experience in it
Just now i've tried

Code: Select all

|^\^+(\w+)+_+(\w+)|
But it didn't worked
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

I think i've got it by myshelf

Code: Select all

/^\^+(\w+)+_+(\(+\w+\))/
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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 »

Ya I got it

Code: Select all

/^\^+(\w+)+_+\(+(\w+)+\)/
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 ??
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You know what pluses do in regex, right?
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

I can decrease 1 "+"

Code: Select all

/^\^+(\w+)+_\(+(\w+)+\)/
but how only 2 ??
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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"
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

nwp wrote:Ya I got it

Code: Select all

/^\^+(\w+)+_+\(+(\w+)+\)/
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! :wink:
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post 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____
)
Last edited by nwp on Mon Feb 19, 2007 10:06 pm, edited 1 time in total.
Post Reply