Reputation: 1307
I need get a results of a expression, but I don't want the expression returned in the results.
Example:
I want get all a-z preceded by THISCANTRETURNJUSTCONDITION...
I want get:
thisismyfirstvalue, thisismysecondvalue, thisismythirdvalue, thisismyfourvalue
from:
thisismyfirstvalueTHISCANTRETURNJUSTCONDITIONhellowworld
thisismysecondvalueTHISCANTRETURNJUSTCONDITIONhellowworld
thisismythirdvalueTHISCANTRETURNJUSTCONDITIONhellowworld
thisismyfourvalueTHISCANTRETURNJUSTCONDITIONhellowworld
How I can do it?
I've tried the following but they aren't right:
[a-z]*(THISCANTRETURNJUSTCONDITION)
[a-z]*(?=THISCANTRETURNJUSTCONDITION)
[a-z]*(=?THISCANTRETURNJUSTCONDITION)
Remember: I don't want the condition returned in the results.
Upvotes: 1
Views: 67
Reputation: 270677
Since you're using PHP and preg_*
functions, this is a job for preg_replace()
if I understand what you're attempting. If I get your intent, you are not looking to pass on matching if THISCANTRETURNJUSTCONDITION
is present, but rather not return it from the string:
echo preg_replace("/^([a-z]*)(THISCANTRETURNJUSTCONDITION)(.*)$/", '$1', $input_string);
//-----------------(Group1)
//--------------------------(Group2 boundary------------)
//-------------------------------------------------------(Group 3 after boundary)
The pattern is replaced by only the contents of Group1 ([a-z]*)
with $1
.
Upvotes: 0
Reputation: 6578
^([a-zA-Z]*?)(?=THISCANTRETURNJUSTCONDITION)
, remove the "A-Z" if you actually want case sensitivity.
Upvotes: 1