1

I'm trying to create a Regex expression to use with preg_replace() to replace a expression that's between two other expressions. The overall idea is:

$new = "stuff";
$original = /*see below*/;
$final = preg_replace('regex expression', $new, $original);

example of $original string:

(...)"dl cm = xxxx opop1 = XYZ opop3 = XXXXXXXXXXXX,XXXX,"thisisit" YYYY = z"(...)

example of the $final string:

(...)"dl cm = xxxx opop1 = XYZ opop3 = XXXXXXXXXXXX,XXXX,"stuff" YYYY = z"(...)

(The X's, Y's, Z's can be any character)

The expression must find the 'opop3 = XXXXXXXXXXXX,XXXX,"' then the following '"' and change the text inside this two delimiters.

I think that I found how to find the first delimiter

(opop3.=(.*)(\,)(.*)(\,)\")

And find the following '"' is not hard, but i don't know how can I change the text between them without touching the delimiters.

(or if someone know other way to do this, please, let me know)

1 Answer 1

1

You can make use of \K (reset search) of PCRE:

$final = preg_replace('/XXXXXXXXXXXX,XXXX,"\K[^"]+/', $new, $original);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, it worked! $final = preg_replace("/opop3.*=.*,.*,"\K[^"]+/", "notinternet", $input_lines);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.