Regex to match \ a574322 in Java
My long line looks like this: \ c53 \ e59 \ c9 \ e28 \ c20140326 \ a4095 \ c8 \ c15 \ a546 \ c11, and I need to find expressions starting with \ a and followed by numbers. For example: \ a574322 And I have no idea how to build it. I cannot use:
Pattern p = Pattern.compile("\\a\\d*");
because \ a is a special character in regex. When I try to group it like this:
Pattern p = Pattern.compile("(\\)(a)(\\d)*");
I am getting an unlocked group error even if there is an even number of parentheses. Can you help me?
Thank you very much for your solution.
You need 4 \
.
2 to tell the regex that this is not a special character but a simple one \
and 2 for each to tell the Java String that these are not special characters. Therefore, you need to represent it in code like this:
"\\\\a\\d*"
This is actually a regular expression \\a\d*
You can use this regex:
\\\\a\\d+
Demo Code
Since in Java you need to double escape \\
once for String and second time for regex mechanism.
You need to change your regex:
Pattern p = Pattern.compile("(\\\\a\\d+)");
Regular expression:
(\\a\d+)
The idea is to escape the backslash and then also escape the backslash for \a
and match numbers as well.
\\(a)[0-9]+
this should work
you can't try your regex on this page or some similar
http://regex101.com/