Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regular expression for 1=1

Tags:

java

regex

I need to look for java regex pattern that finds input string in the format of 1=1 where prefix of "=" should have same count of digits with the suffix. Also here both prefix & suffix values should be the same like 1=1, 11=11, 223=223. Values like 1=2, 3=22, 33=22 should not match the pattern

Can we have a general pattern to satisfy above rules.

like image 766
erdarun Avatar asked Jan 21 '14 09:01

erdarun


People also ask

What does 1 mean in regex in Java?

It indicates a specific number, lists of numbers, or range (to infinity). A number indicates an exact number of occurrences. The coma is used to indicate multiple (when between numbers) or a range to infinity (when not followed by a number.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .

Which regex matches one or more digits?

+: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits.

What is ?: In regex?

It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.


1 Answers

Use a back reference:

(\d+)=\1\b

of course, in java you need to escape the back slashes:

"(\\d+)=\\1\\b"
like image 158
Bohemian Avatar answered Nov 14 '22 02:11

Bohemian