GorillaApe
GorillaApe

Reputation: 3641

Help with java regular expression

"^[\\s]*DISPLAY.*?(\".*?\").*?\\."

I have the above regular expression. However i have a problem with it.

DISPLAY AC-YEAR LINE 2 POSITION 68 REVERSE.

This string isn't captured as it should.

DISPLAY "EATING.FOOD" LINE 13 POSITION 31 REVERSE.

This is captured successfully.

I cant figure how I should write the regular expression.

"^[\\s]*DISPLAY.*?(\".*?\")  *,?,??       .*?\\.

Putting quantifiers after the ) doesn't work. It doesn't even capture the group.

Upvotes: 0

Views: 162

Answers (3)

Mihai Toader
Mihai Toader

Reputation: 12253

The regex needs a pair of quotes (") present after the DISPLAY word. That's why the first one doesn't work.

If you want the first non white space word after the DISPLAY (everything between DISPLAY and LINE this should work:

^[\\s]*DISPLAY\\s*?(\".*?\"|.*?)\\s*?LINE.*?\\.

Upvotes: 2

Mike C
Mike C

Reputation: 3117

This should work to capture AC-YEAR and "EATING.FOOD", while matching the entire line:

^\\s*DISPLAY\\s(.*?)\\s.*\\.$

Note that the $ matches the end of the line, so the literal dot \\. will only be matched at the end.

If you want to capture everything after DISPLAY, this should do it:

^\\s*DISPLAY\\s(.*?)\\.$

Upvotes: 0

Manoj
Manoj

Reputation: 5612

The first string did not match the regex because (\".*?\") matches anything between start quot and end quot, whicxh is not in your string. I suggest you test your regex here http://www.regexplanet.com/simple/index.html

Upvotes: 0

Related Questions