2

I need to take a string and extract every instance of a pattern and only the pattern.

String test = "This is a test string to experiment with regex by separating every instance of the word test and words that trail test";

So now the pattern would have to find the word test as well as any words ahead and behind it that are not test. So basically it would have to result in 3 instances of this pattern being found.

The 3 results that I'm expecting are as follows:

  1. This is a test string to experiment with regex by separating every instance of the word
  2. test and words that trail
  3. test

I've played around with postive lookahead and negative lookahead on gskinner but no luck yet.

5
  • Is this even possible with Regex? May have to design it your own.
    – Mawia
    Jan 18, 2013 at 10:54
  • 1
    Funny thing is that I was designing it on my own for 2 weeks before I decided to change over to doing it with regex lol
    – user1191027
    Jan 18, 2013 at 10:57
  • 1
    Shouldn't the second and third be string to experiment with regex by separating every instance of the word test and words that trail and that trail test respectively? Jan 18, 2013 at 11:46
  • Actually, now that I think of it I think you're right. Maybe post an alternative answer with the above implementation if possible?
    – user1191027
    Jan 18, 2013 at 12:11
  • A pattern search starts, where the last match ends. This means you can't find overlapping matches with regex alone. You can do so by looking for test and start searching from there on, but that makes regex redundant in your case. Jan 19, 2013 at 14:33

2 Answers 2

3

Try this

(\s*\b(?!test\b)[a-z]+\b\s*)*test(\s*\b(?!test\b)[a-z]+\b\s*?)*

See it here on Regexr.

In Java, I would replace [a-z] with \p{L}, but regexr does not support Unicode properties. \p{L} is a Unicode code point with the property letter, this will match every letter in any language.

Explanation:

(\s*\b(?!test\b)[a-z]+\b\s*)* is matching a series of words that are not "test". This is ensured by the negative lookahead assertion (?!test\b).

test is matching "test"

and at the end the same again: match a series of words that are not "test" with again (\s*\b(?!test\b)[a-z]+\b\s*?)*

2
  • "In Java from Version 7..." - I don't get that. Unicode support was much improved in Java 7, but it has always supported \p{L}.
    – Alan Moore
    Jan 18, 2013 at 11:40
  • @AlanMoore removed that limitation, I know the there was a major improvement in 7, but I was not sure and did not check if \p{L} worked before version 7.
    – stema
    Jan 18, 2013 at 12:32
0

To follow up my comment, I could imagine splitting your test string with the pattern \btest\b and then join the string parts left and right

String parts[] = test.split("\btest\b", -1);
for (int i = 0; i < parts.length - 1; ++i)
    System.out.println(parts[i] + "test" + parts[i + 1]);

Your Answer

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