Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm looking to extract values from matched pattern in Java

Tags:

java

regex

I'm using this regex:

([\w\s]+)(=|!=)([\w\s]+)( (or|and) ([\w\s]+)(=|!=)([\w\s]+))*

to match a string such as this: i= 2 or i =3 and k!=4

When I try to extract values using m.group(index), I get: (i, =, 2, **and k!=4**, and, k, ,!=, 4).

Expected output: (i, =, 2, or, i, =, 3, and, k , !=, 4) How do i extract the values correctly?

P.S. m.matches() returns true.

like image 961
abhi5306 Avatar asked Mar 28 '26 01:03

abhi5306


1 Answers

you are trying to match with a regexp on an expression...you might want to use a parser, because this regexp (when you have it) can't be extended further..but a parser can be extended at any time

for example, consider using antlr (ANTLR: Is there a simple example?)

like image 69
Zoltán Haindrich Avatar answered Mar 29 '26 14:03

Zoltán Haindrich