Matheus Gontijo
Matheus Gontijo

Reputation: 1307

Regex return in result

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

Answers (3)

Michael Berkowski
Michael Berkowski

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

ennuikiller
ennuikiller

Reputation: 46965

Try this:

/^(.*?)THISCANTRETURNJUSTCONDITION/

Upvotes: 1

^([a-zA-Z]*?)(?=THISCANTRETURNJUSTCONDITION), remove the "A-Z" if you actually want case sensitivity.

Upvotes: 1

Related Questions