1

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

1
  • Are the texts between two " some special texts that have a special meaning? Therefor @nu11p01n73R 's solution would not work correctly, as it doesn't take in account, that in the first part there could already be a " without a following ; Commented Jan 27, 2015 at 12:06

4 Answers 4

4

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

5
  • :-) not mistakenly. Revert it back if you don't want ... no, don't do that. Commented Jan 27, 2015 at 12:29
  • not the good answer. It's the right answer because your's will fail if more than one semi colons present inside the quotes.. Commented Jan 27, 2015 at 12:31
  • Great, the last one works for me, that was exactly what i'm looking for!! Thank you very much. It seemed to be that i'm not that fimiliar with all regex-Expression as I thought ;) And i tried to access the matches with "$1:$2" and not with double back slash.... Thanks a lot! Commented Jan 27, 2015 at 14:05
  • Hey, your answer helps me a lot and it works fine up to now... I found a possible combination in which this solution doesn't work correctly... I tried to modify your solution to run all variations but without any success.. If you have the following string 'asbas;asd:;asd;asdadasd;"asd:adsas' the regex replace all ; before the double qoutes... i would expect that the regex do nothing. It should only replace the ; within two double quotes. So i hope you can help me again! Thank's a lot. Commented Jul 19, 2015 at 15:49
  • The m pattern modifier is useless if there are no anchors in the pattern -- like ^ or $. Commented Jul 2, 2022 at 12:06
1

A simple understandable solution could be the use of preg_replace_callback:

$str = preg_replace_callback('/"[^"]+"/',
       function ($m) { return str_replace(";", ":", $m[0]); },
       $str);

"[^"]+" captures the quoted stuff to $m[0] where ; is replaced by :

See test at eval.in (link will expire soon)

0
1
;(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)

Try this.Replace by :.See demo.

https://www.regex101.com/r/bC8aZ4/16

0
-1

What about string replace?

str_ireplace(';";', ':";', $orig_string);

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

2
  • In programming there's no one solution for one problem, you give 10 programmers a problem, the approach to the problem will be different with the same results, yet i see you got a thumb down for giving a solution to a problem with a different approach. Commented Jan 27, 2015 at 12:12
  • Your Solution replaces only a semicolon if it is followed by a " and another semicolon, but the second occurence in the example is not replaced, as it follows another pattern. Commented Jan 27, 2015 at 12:23

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.