vinit saha
vinit saha

Reputation: 57

Javacc regex to skip all characters unless a specific character found

I want to skip all characters unless I find a specific characters. For e.g. for below string sequence:

--dfdfdffdfdfefsd@ : 
sdsdsdsadsad
hkkldjsfsdf , ! : 
%ABC-1.[0-7]

I want a javacc regex which skips all characters unless it finds %ABC-1.[0-7].

The normal regex for this would be .*?%ABC-1.[0-7], but for javacc I found something like ~[]?%ABC-1.[0-7]. But it is not working.

Could you please suggest what should be correct javacc regex equivalent to normal regex .*?%ABC-1.[0-7]?

Thanks

Upvotes: 0

Views: 670

Answers (1)

Theodore Norvell
Theodore Norvell

Reputation: 16221

If you want one regular expression that matches all characters up to and including %ABC-1.[0-7] you can do this.

TOKEN { <FOO : (~[])* "%ABC-1.[0-7]" > }

If you want one regular expression that matches up to, but not including %ABC-1.[0-7], that can't be done, since JavaCC does not allow right context to be used. What you can do instead is this:

TOKEN { <CHAR : ~[]> }
TOKEN { <FOO : "%ABC-1.[0-7]" > }

In either case, you'll likely want to change the state after the FOO token is matched.

Upvotes: 1

Related Questions