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.

+3


source to share


4 answers


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*

0


source


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.

+3


source


You need to change your regex:

Pattern p = Pattern.compile("(\\\\a\\d+)");

      

Regular expression:

(\\a\d+)

      

enter image description here

The idea is to escape the backslash and then also escape the backslash for \a

and match numbers as well.

+3


source


\\(a)[0-9]+

this should work

you can't try your regex on this page or some similar

http://regex101.com/

-1


source







All Articles