ruby - Replace all colon character that does not occur after specific word pattern -


for given text

pack: box five: dozen: liquor: jugs ::: :

i want replace : ; occur after words ack , zen. output expect here is

pack: box five; dozen: liquor; jugs ;;; ;

note : after ack , zen untouched whereas else replaced.

please me. able match (ack|zen):. giving me matches ack: , zen:. dont know how negate , construct regex.

note: using ruby.

you can use negative look-behind here , use gsub:

puts "pack: box five: dozen: liquor: jugs ::: :".gsub(/(?<!ack|zen):/, ";") 

ideone demo

results:

pack: box five; dozen: liquor; jugs ;;; ; 

you can use look-behind here because both parts of same size (3 chars).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -