Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape string to be passed as regex

Tags:

I would like to create a function that creates regex matching an arbitrary string given at the input. For example, when I feed it with 123$ it should match literally "123$" and not 123 at the end of the string.

def convert( xs: String ) = (xs map ( x => "\\"+x)).mkString                   val text = """ 123 \d+ 567 """                                                 val x = """\d+"""                                                             val p1 = x.r                                                                  val p2 = convert(x).r                                                          println( p1.toString )                                                          \d+ // regex to match number                                                 println( ( p1 findAllIn text ).toList )                                         List(123, 567) // ok, numbers are matched                                    println( p2.toString )                                                          \\\d\+ // regex to match "backshash d plus"                                  println( ( p2 findAllIn text ).toList )                                         List() // nothing matched :(                                                

So the last findAllIn should find \d+ in text, but it doesn't. What's wrong here?

like image 693
Jakub M. Avatar asked Aug 02 '12 09:08

Jakub M.


People also ask

How do you escape a string in regex?

The backslash in a regular expression precedes a literal character. You also escape certain letters that represent common character classes, such as \w for a word character or \s for a space.

What characters must be escaped in regex?

Escape all non-alphanumeric characters.

Do I need to escape slash in regex?

A slash symbol '/' is not a special character, but in JavaScript it is used to open and close the regexp: /... pattern.../ , so we should escape it too.

What does escape in regex mean?

Now, escaping a string (in regex terms) means finding all of the characters with special meaning and putting a backslash in front of them, including in front of other backslash characters. When you've done this one time on the string, you have officially "escaped the string".


2 Answers

You can use Java's Pattern class to escape strings as regular expressions. See http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#quote%28java.lang.String%29

For example:

scala> Pattern.quote("123$").r.findFirstIn("123$") res3: Option[String] = Some(123$) 
like image 198
Kim Stebel Avatar answered Nov 16 '22 11:11

Kim Stebel


Just to bring more attention to Harold L's comment above, if you want to do this with a Scala library you can use:

import scala.util.matching.Regex Regex.quote("123$").r.findFirstIn("123$") 
like image 30
Brideau Avatar answered Nov 16 '22 13:11

Brideau