Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace non-duplicate character in string

HI I want to replace a forward slash by a space, but only if it appears once.

 str_replace_all( 'NOOOOO//ABCDEFGHI/asdfasd//sdkksksks', "/(?!=/)", " ")

Here I want the output to be: NOOOOO//ABCDEFGHI asdfasd//sdkksksks

like image 774
user1420372 Avatar asked Dec 06 '25 05:12

user1420372


1 Answers

Try the following option with sub:

input <- "NOOOOO//ABCDEFGHI/asdfasd//sdkksksks"
gsub("(?<!/)/(?!/)", " ", input, perl=TRUE)

[1] "NOOOOO//ABCDEFGHI asdfasd//sdkksksks"

The strategy here is to use the pattern (?<!/)/(?!/), which matches a single forward slash which it surrounded on both sides by anything other than another forward slash.

like image 101
Tim Biegeleisen Avatar answered Dec 08 '25 19:12

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!