Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert javascript regex to safe java regex?

strOutput.replace("/{{[^]*?}}/g","");

Is there a way to convert JavaScript regexes to Java-safe regexes?

The above statement gives me the error:

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

I'm not all that familiar with regex, so I could use some guidance.

Thanks!

like image 864
rockit Avatar asked Jan 24 '11 23:01

rockit


People also ask

What does \\ mean in Java regex?

String regex = "\\."; Notice that the regular expression String contains two backslashes after each other, and then a . . The reason is, that first the Java compiler interprets the two \\ characters as an escaped Java String character. After the Java compiler is done, only one \ is left, as \\ means the character \ .

Which regex does Java use?

The Java regex package implements a "Perl-like" regular expressions engine, but it has some extra features like possessive quantifiers ( . *+ ) and variable-length (but finite) lookbehind assertions). On the other hand, it misses a few features Perl has, namely conditional expressions or comments.

Does Java have regex?

Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java.

Does JavaScript support regex?

Regular expressions are patterns that provide a powerful way to search and replace in text. In JavaScript, they are available via the RegExp object, as well as being integrated in methods of strings.


2 Answers

Get rid of the forward slashes. You don't need those in Java. Also, Java's flavor of regex doesn't recognize switches like /g and /i; those are controlled by constants in java.util.regex.Pattern.

The only Javascript regex switches that make sense in the Java world are /i and /m. These map to Pattern.CASE_INSENSITIVE and Pattern.MULTILINE (you can use these switches when creating a regex from the Pattern class, or you can use them inline -- I'll show this later).

The /g doesn't map to anything, but you can control replace behavior by using String.replaceAll versus String.replaceFirst.

To get your code to work, you'd have to do something like this:

strOutput.replaceAll("{{[^]*?}}", "");

If you wanted to use switches, you need to do add something like (?i) to the beginning of the regex.

You can't use String.replace because it takes in a CharSequence for the first argument and not a regex.

Also keep in mind that the "quick regex" methods offered by the String class may not work like you expect it to. This is because when you specify a pattern (let's say abc) as a regex for matches for example, the actual pattern seen by Java is ^abc$. So abc will match, but abcd will not.

There is more information here.

like image 118
Vivin Paliath Avatar answered Sep 21 '22 02:09

Vivin Paliath


Get rid of "/" and "/g" at the start and the end of regex. Then you need to escape every "\" occurrence like so: "\\".

The "g" part means global. This is controlled in how you use regex in Java as opposed to in the regex string.

like image 40
Konstantin Komissarchik Avatar answered Sep 21 '22 02:09

Konstantin Komissarchik