0

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?

4
  • 2
    "Concatenation of the two strings can't be done." why not? Commented Nov 9, 2015 at 13:00
  • Questions requesting library recommendations are off topic. As for doing it yourself, you are expected to at least attempt it yourself first. Show us you've put effort into this. What problems are you having implementing this yourself? Commented Nov 9, 2015 at 13:03
  • Because they're scattered over a large XML file. I need to visit every node and treat the text in every node as a piece of one string. I don't know in which node the match will start and how much nodes will it take to complete the match. But I need to remember the nodes where the match happened and the positions of start/end in the node's text. Commented Nov 9, 2015 at 13:03
  • What you're asking doesn't really make sense. You're treating regular expressions as something you can calculate over a stream, but you require backtracking to handle them. Commented Nov 9, 2015 at 13:06

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.