简体   繁体   中英

Regular expression for finding suffixes of first occurrence of string

I'm trying to create regular expression in Java to match first occurrence of some string (let's say A) together with all following occurrences of other string (let's say C) after the first one.

So for example:

XAXXAXXCXC

In this case, following matches should be produced:

AC (position 1 and 7)
AC (position 1 and 9)

I have partially succeeded with positive lookbehind (see below), but the problem is that it is not greedy, so A at position 4 is taken instead of the first one:

(?<=(A).{0,20})C

Finally I solved this by combination of possitive and negative lookbehind. But with a limitation, that the A must be before C to some defined length (in this case 20).

(?<=(?<!A.{0,20})(A).{0,20})C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM