Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following problem: I want to replace (in php) a special character, but only if it's between two other characters. It tried to find a solution with with preg_replace but it doesn't work.

I want to replace every ; with a : which is between the " The Examples:

$orig_string= 'asbas;"asd;";asd;asdadasd;"asd;adsas"'

result should be:

'asbas;"asd:";asd;asdadasd;"asd:adsas"'

I tried several regexes but without any succes...

Two examples i tried:

$result = preg_replace('(?<=")(.*)(;)(.*)(?=")',':', $str);

$result = preg_replace('.*".*(;).*"',':', $str);

Can anybody help me?

Thanks a lot

V

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
367 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need not use look arounds here. It can be written as

("[^";]*);([^"]*")

replace with 1:2

Regex Demo

Test

preg_replace ("/("[^";]*);([^"]*")/m", "\1:\2", 'asbas;"asd;";asd;asdadasd;"asd;adsas"' );
=> asbas;"asd:";asd;asdadasd;"asd:adsas"

Update:

;(?!(?:"[^"]*"|[^"])*$)

Just replace the matched ; with :

DEMO


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...