Is it possible to write a Java Regex sequence to match two identical sequences within a string. In other words given the string
near[2015-12-1] far[2015-12-1]
I want to match all strings where the value inside the first square brackets is equal to the one in the second square bracket and the strings outside the square brackets are near[] far[]
.
near[2015-12-1] far[2015-12-1] MATCH
near[2015-12-3] far[2015-12-1] NO MATCH
near[2015-12-1] far[2014-12-1] NO MATCH
near[2015-12-3] far[2015-12-3] MATCH
foo[2015-12-1] bar[2015-12-1] NO MATCH
Is this possible?
Use capturing group and you should refer those captured characters by back-reference.
"^near\\[(.*?)\\]\\sfar\\[\\1\\]$"
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With