Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match unescaped special characters only

Tags:

java

regex

I'm trying to come up with a regular expression that can match only characters not preceded by a special escape sequence in a string.

For instance, in the string Is ? stranded//? , I want to be able to replace the ? which hasn't been escaped with another string, so I can have this result : **Is Dave stranded?**

But for the life of me I have not been able to figure out a way. I have only come up with regular expressions that eat all the replaceable characters.

How do you construct a regular expression that matches only characters not preceded by an escape sequence?

like image 796
Olaseni Avatar asked May 09 '11 13:05

Olaseni


People also ask

What is the regex for special characters?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).

How do you escape special characters?

Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped. Note: If you use braces to escape an individual character within a word, the character is escaped, but the word is broken into three tokens.

What is escape regex?

Escape converts a string so that the regular expression engine will interpret any metacharacters that it may contain as character literals.

How do you escape special characters in regex python?

escape() was changed to escape only characters which are meaningful to regex operations. Note that re. escape will turn e.g. a newline into a backslash followed by a newline; one might well instead want a backslash followed by a lowercase n.


1 Answers

Use a negative lookbehind, it's what they were designed to do!

(?<!//)[?]

To break it down:

(
    ?<!    #The negative look behind.  It will check that the following slashes do not exist.
    //     #The slashes you are trying to avoid.
)
[\?]       #Your special charactor list.

Only if the // cannot be found, it will progress with the rest of the search.

I think in Java it will need to be escaped again as a string something like:

Pattern p = Pattern.compile("(?<!//)[\\?]");
like image 102
Buh Buh Avatar answered Sep 22 '22 10:09

Buh Buh