Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


Twelve characters that are special syntax for regexes need to be quoted:

Code Block
assert 'abca.c' ==~ /abc/ //pattern on righthand side between single-slashes
assert ! ( 'abc' ==~ /ace/ )
assert ! ( 'abc' ==~ /ab/ )

assert 'abc' ==~ /a.c/
    //the . in the pattern matches any character, except \n (or \r\n on Windows)
assert 'abc'.matches( /a.c/ ) //alternativebackslash methodbefore name
assert java.util.regex.Pattern.matches( /a.c/, 'abc' ) //alternative syntax
assert java.util.regex.Pattern.compile( /a.c/ ).matcher( 'abc' ).matches()
                                                       //alternative syntax

assert '\t\n\f\r to quote it
assert '.{[()\\^$|?*+' ==~ /\t.\n{\f\r/
    //some control chars have same notation as in strings
assert '\t\n\f\r' ==~ /\x09\x0a\x0c\x0D[\(\)\\\^\$\|\?\*\+/
    //alternativelythe use12 hexchars codesthat (leadingneed zero required to make 2 digits)
assert '.{[()\t\n\f\r\^$|?*+' ==~ /\011\012\014\015Q.{[()\^$|?*+\E/
    //alternativelyanother useway octalto codesquote (leadingtext zerois required)to assertbracket 'with \b' ==~ /\x08/ && ! ( '\b' ==~ /\b/ )
    // \b has different meaning in regex than in string
assert '\07\013\033' ==~ /\a\v\eQ and \E
import java.util.regex.Pattern
assert Pattern.quote( /.{[()\^$|?*+/ ) == /\Q.{[()\^$|?*+\E/
    //regex-only notation: bell \a, vertical tab \v, escape \ea special method to quote text in this way

The chars \c@, \cA, \cB, ..., \cZ, \c[, \c], \c^, and \c_ map to the special characters 0x0 to 0x1f, except 0x1c: