Skip to content

Illegal escaping on a Perl-style hex notation example on https://dev.java/learn/regex/intro/ #272

Description

@willy-b

On the latest version of https://dev.java/learn/regex/intro/ (archived as is at https://megalodon.jp/2026-0814-0023-31/https://dev.java:443/learn/regex/intro/ , snippet may not show on the archiver but should be in the html )

  • For the Perl-style hex notation example for matcher patterns:
String hexPattern = "\x{" + Integer.toHexString(codePoint) + "}";
Image

The example appears to be invalid due to illegal escaping (I have reported other typos in this series (at #212 ) , but did NOT author or modify this content):

jshell> String hexPattern = "\x{" + Integer.toHexString(codePoint) + "}";
|  Error:
|  illegal escape character
|  String hexPattern = "\x{" + Integer.toHexString(codePoint) + "}";
|                        ^

I think it should be:

String hexPattern = "\\x{" + Integer.toHexString(codePoint) + "}";

so that one, using "\x{...}" instead of "\x{...}" can successfully match:

jshell> int codePoint = 0x6771; 
codePoint ==> 26481

jshell> String hexPattern = "\\x{" + Integer.toHexString(codePoint) + "}";
hexPattern ==> "\\x{6771}"

jshell> "\u6771"
$37 ==> "東"

jshell> "\u6771".matches(hexPattern)
$38 ==> true

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions