Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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

share|improve this question
    
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 ; – Roland Bär Jan 27 at 12:06

4 Answers 4

up vote 4 down vote accepted

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

share|improve this answer
    
:-) not mistakenly. Revert it back if you don't want ... no, don't do that. – Avinash Raj Jan 27 at 12:29
    
@AvinashRaj Let it be there :P Its actually a good answer :D – nu11p01n73R Jan 27 at 12:30
    
not the good answer. It's the right answer because your's will fail if more than one semi colons present inside the quotes.. – Avinash Raj Jan 27 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! – Der_V Jan 27 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. – Der_V Jul 19 at 15:49

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)

share|improve this answer
;(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)

Try this.Replace by :.See demo.

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

share|improve this answer
    
Your Solution replaces the first occurence, but not the second... – Roland Bär Jan 27 at 12:22
    
@RolandBär edited.try now – vks Jan 27 at 15:18

What about string replace?

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

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

share|improve this answer
    
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. – Letme SuckitBaby Jan 27 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. – Roland Bär Jan 27 at 12:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.