0

As title says, i'd like to get the portion of the pattern that is being matched partially by the input; example:

Pattern: aabb Input string: "aa"

At this point, i'll use hitEnd() method of Matcher class to find out if the pattern is being matched partially, like shown in this answer, but i'd also like to find out that specifically "aa" of "aabb" is matched. Is there any way to do this in java?

jaco0646
  • 15,303
  • 7
  • 59
  • 83
user561787
  • 15
  • 3

2 Answers2

0

This may be dirty, but here We go...

Once you know that some string hitEnd, do a second processing:

  1. Remove the last character from the string
  2. Search with the original regex
  3. If It matches, then you are over and you have the part of the string
  4. If not, go to 1 and repeat the whole process until you match

If test strings can be long, performance may be a problem. So instead of positions from last to first, try searching for blocks.

For example, considering a string of 1,000 chars:

  1. Test 1000/2 characters: 1-500. For this example, we consider it matches
  2. Test for first 500 chars + 500/2 (1-750 positions). For this example, We consider It does not match. So we know that the position must be placed from 500 to 750
  3. Now test 1-625 ((750+500)/2)... If it matches, the positions must exist between 625-750. If it does not match, It must be from 500 to 625
  4. ...
Julio
  • 5,208
  • 1
  • 13
  • 42
0

There is no such function in Matcher class. However you could achieve it for example in this way:

public String getPartialMatching(String pattern, String input) {
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(input);
    int end = 0;
    while(m.find()){
        end = m.end();
    }
    if (m.hitEnd()) {
        return input.substring(end);
    } else {
        return null;
    }
}

First, iterate over all matched parts of string and skip them. For example: input = "aabbaa" m.hitEnd() will return false without skipping aabb. Second, validate if the left part of the string partially matches.

uli
  • 671
  • 6
  • 15