Anatoliy Kmetyuk
Anatoliy Kmetyuk

Reputation: 728

Regex over multiple strings

I need to use a regex to start matching in one string and end matching in another string. For example, given a regex:

[A-Z]+[0-9]+

and two strings:

String s1 = "aaaABC";
String s2 = "1245aaa";

It should be possible to do as follows:

regex.feed(s1);  // returns the start of the match at 3 and end at 5
regex.feed(s2);  // returns continuation of the match at 0 and end at 4

Concatenation of the two strings can't be done.

Any ready-made libraries to do that? Any ideas on how to make one myself?

Upvotes: 0

Views: 66

Answers (1)

Stephen C
Stephen C

Reputation: 718678

Any ready-made libraries to do that?

AFAIK, No. This is a very unusual requirement.

Any ideas on how to make one myself?

Well the obvious solution is to concatenate the strings.

But another solution might be to create a custom CharSequence class that delivers the the characters of one string followed by the second string. Then pass an instance of that class as the parameter for the Pattern.matcher(...).


Based on your comment, you actually want to search a sequence of strings extracted from an XML DOM. Implementing a CharSequence that supports this efficiently could be a bit of a challenge.

Upvotes: 2

Related Questions