mercredi 29 août 2018

Java Pattern regex search between strings

Given the following strings (stringToTest):

1 -

G2:7JAPjGdnGy8jxR8[RQ:1,2]-G3:jRo6pN8ZW9aglYz[RQ:3,4]

2 -

G2:7JAPjGdnGy8jxR8[RQ:3,4]-G3:jRo6pN8ZW9aglYz[RQ:3,4]

And the Pattern:

Pattern p = Pattern.compile("G2:\\S+RQ:3,4");
if (p.matcher(stringToTest).find())
{
    // Match
}

For string 1 I DON'T want to match, because RQ:3,4 is associated with the G3 section, not G2, and I want string 2 to match, as RQ:3,4 is associated with G2 section.

The problem with the current regex is that it's searching too far and reaching the RQ:3,4 eventually in case 1 even though I don't want to consider past the G2 section.

It's also possible that the stringToTest might be (just one section):

G2:7JAPjGdnGy8jxR8[RQ:3,4]

The strings 7JAPjGdnGy8jxR8 and jRo6pN8ZW9aglYz are variable length hashes.

Can anyone help me with the correct regex to use, to start looking at G2 for RQ:3,4 but stopping if it reaches the end of the string or -G (the start of the next section).

I've been trying for hours, thanks in advance.

Steve.

Aucun commentaire:

Enregistrer un commentaire