开发者

Regex Replace: Match hypen not preceded by slash

开发者 https://www.devze.com 2023-01-04 08:28 出处:网络
How can i replace Hypen\'s inside a string, but ignore hypens preceded by a slash eg: \"Just-Testing-A-String-\\--But i want to leave this hyp开发者_运维技巧en\"If you want to keep only one hyphen tha

How can i replace Hypen's inside a string, but ignore hypens preceded by a slash eg: "Just-Testing-A-String-\--But i want to leave this hyp开发者_运维技巧en"


If you want to keep only one hyphen that follows the \, then you can use regex

(?<!\\)-

UPDATE: Actually I don't believe that it is possible to create such a rexeg, because in this case you would have to do two replacements: one is for

'-' to ' ' 

and another one is for

'\-' to '-', 

so you need to run two replacements. The only thing I can think of is if you OK with replacing '\' with ' ' also. Then you can use the following regex

((?<!\\)-)|(\\(?=-))


Try something like this:

regex ([^\\])-

replace with $1YOURSTRING


What language are you using? I would go about this in different ways depending on the language being used.

The backslash in your example is actually creating an invalid escape character, "-" will throw an error in most languages. Could you mean a foreword slash, "/"?

0

精彩评论

暂无评论...
验证码 换一张
取 消