i have a following paragraph.

++ a
xxx
xxxx

++ b
yyy
yyyy
yyyyy

++ c
zz
zzz

i need to delete this line.

++ b
yyy
yyyy
yyyyy


What is the regular expression i want to use?

Recommended Answers

All 7 Replies

Something like:

preg_replace('%(^++ b.*?)(++ c)%', '$2', $yourfile);

but you'll have to do some test to see if it is correct.

Something like:

preg_replace('%(^++ b.*?)(++ c)%', '$2', $yourfile);

but you'll have to do some test to see if it is correct.

Thankyou for your reply. But in my above paragraph , "b" is the only known variable. a,c and others are unknown.(changable).

What is the best solution for this problem? Please help me.

Just remove the c from the pattern. As long as the line will start with ++ it will still work.

Just remove the c from the pattern. As long as the line will start with ++ it will still work.

Even if i remove c, preg_replace function replace the following text including ++.

++ b
yyy
yyyy
yyyyy

++

i only need to delete

++ b
yyy
yyyy
yyyyy

Any Solutions please?

My test show you should use this:

$result = preg_replace('/(^\+\+ b.*?)(^\+\+)/sm', '${2}', $subject);
commented: Very useful +1

My test show you should use this:

$result = preg_replace('/(^\+\+ b.*?)(^\+\+)/sm', '${2}', $subject);

thanks for your reply. what this syntax "${2}" tells ?

Basically, the first pattern matches two parts $1 and $2, which are replaced by the second ($2). If you want to learn more, I can recommend this website.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.