Hi,
I'm creating a script that allows user to pass a parameter string:
anystring_here%var%
or
here_is_the_string%var%_
I will replace the %var% to (.*) so those strings above will be a regex:
anystring_here(.*)
or
here_is_the_string(.*)_
thus, when I preg_match those regex to some text, it can take the %var% portion.
text example:
anystring_hereXYZABC -> will take 'XYZABC' with first regex
here_is_the_stringABCDE_more -> will take 'ABCDE' with 2nd regex
the problem with 2nd regex, if the text like this:
here_is_the_stringABCDE_more_and_more -> it will take 'ABCDE_more_and'
I know that I need to put (.*?) to stop greedy fetching, but if it applies on 1st regex, it won't take anything.
anystring_here(.*?) won't take 'XYZABC' from anystring_hereXYZABC
so, is there any solution to get both regex's work?
Thanks.
take anything at the end of string with some rules
Moderator: General Moderators
confused by your examples:
Read http://regexadvice.com/blogs/mash/default.aspx">
and http://regexadvice.com/forums/thread/37499.aspx
to write better regex questions
give haystacks
give the expected matches in each haystack
Read http://regexadvice.com/blogs/mash/default.aspx">
and http://regexadvice.com/forums/thread/37499.aspx
to write better regex questions
give haystacks
give the expected matches in each haystack
Code: Select all
<?php
$hay = 'here_is_the_string_XYZABC
or
here_is_the_string_XYZABC_blabla
or
here_is_the_string_XYZABC_bla_bla';
$pat = '~here_is_the_string_(XYZABC)~';
preg_match_all($pat, $raw, $out);
echo '<pre>';
print_r($out[1]);
echo '</pre>';
?>Thanks for your reply, vapoorize, i change your code to suit my need and to explain my original problem:
the code above will result:
ABCDEF
DEFABC_blabla
XYZABC_bla_bla
but what I want is the result like this:
ABCDEF
DEFABC
XYZABC
what needs to be changed in the pattern?
thanks.
Code: Select all
<?php
$hays[] = 'here_is_the_string_ABCDEF';
$hays[] = 'here_is_the_string_DEFABC_blabla';
$hays[] = 'here_is_the_string_XYZABC_bla_bla';
$pat = '~here_is_the_string_(.*)~';
foreach($hays as $hay)
preg_match_all($pat, $hay, $out);
?>ABCDEF
DEFABC_blabla
XYZABC_bla_bla
but what I want is the result like this:
ABCDEF
DEFABC
XYZABC
what needs to be changed in the pattern?
thanks.
Regexes are SPECIFIC to the data, did you read the links I posted earlier? I doubt it. You're lucky I'm not completely ignoring your post, but I figure I would repeat myself again 1 more time and maybe you'll get the hint. Post some real data.
See a better question would be to specify some rules or criteria of what should be matched, you didn't. So I'll just look at your data and come up with my own criteria. It looks like you want the capital letters in the haystack to be matched, so:
~[A-Z]~
Your attempt:
~here_is_the_string_(.*)~
the * symbol is greedy, it will never stop eating, you could make it lazy or maybe add some kind of rule to determine where it will stop eating, but then again this criteria is unknown
Read: http://www.regular-expressions.info/repeat.html
It looks like you want to match this:
~here_is_the_string_[^_]*~
See a better question would be to specify some rules or criteria of what should be matched, you didn't. So I'll just look at your data and come up with my own criteria. It looks like you want the capital letters in the haystack to be matched, so:
~[A-Z]~
Your attempt:
~here_is_the_string_(.*)~
the * symbol is greedy, it will never stop eating, you could make it lazy or maybe add some kind of rule to determine where it will stop eating, but then again this criteria is unknown
Read: http://www.regular-expressions.info/repeat.html
It looks like you want to match this:
~here_is_the_string_[^_]*~
Re: take anything at the end of string with some rules
Look, I was trying to explain my problem in the first post, it's completely there but you said you're confused so I tried to slice it with a hope we can end up solving my real problem (again as I said in 1st post).
You always say "give the real data, give the haystacks", they were there:
I know regex is specific to the data, but please read my 1st post:
Hey, sorry to say that, I AM thanking for your willing to reply here, but you seem to dictate too much here.
You always say "give the real data, give the haystacks", they were there:
it represents my real data and yes, I can say it's my real data, or do I need to create so many examples? I'm asking about regex, so my examples above is representing the pattern of my real data. When I asked "how to get 'ABCDEF' ?", it means how to get that part from that string's pattern because I'm asking regex here, if I'm not asking about regex, maybe somebody will answer "use substr!", case closed.$hays[] = 'here_is_the_string_ABCDEF';
$hays[] = 'here_is_the_string_DEFABC_blabla';
$hays[] = 'here_is_the_string_XYZABC_bla_bla';
I know regex is specific to the data, but please read my 1st post:
lastly, you're telling me about (.*) is greedy, I did state it in my 1st post, it seems you who didn't read my question and then push me to ask about another thing instead of my original question.I'm creating a script that allows user to pass a parameter string:
anystring_here%var%
or
here_is_the_string%var%_
Hey, sorry to say that, I AM thanking for your willing to reply here, but you seem to dictate too much here.