4

I found a similar question here. However I didn't get it working:

I have a string like "my_token_string" and need a regex to return the tokens "my_", "_token_", and "_string".

Please note that I am not able to change the java-code because it's part of another software. The only thing I can do is specify the pattern and the group to capture :-)

This is what I have tested:

String p = "(?=(_[^_]*_?))";
int group = 1;
String test = "my_token_string";

Matcher m = Pattern.compile(p).matcher(test);
while (m.find()) {
    System.out.println(m.group(group));
}

But of course this returns only the tokens "_token_" and "_string".

Community
  • 1
  • 1
Biggie
  • 7,037
  • 10
  • 33
  • 42

2 Answers2

4

You can try with "(?=((^|_).+?(_|$)))". As group number use 1.

It will let token start with _ or beginning of input (^) and end it with _ or end of input ($). Instead of .+? you can use [^_]+ but I prefer this version.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
3

You can achieve this with RegEx: (?=((?:_|^)[^_]*+(?:_|$)))
Explained demo here: http://regex101.com/r/tB0bZ4

CSᵠ
  • 10,049
  • 9
  • 41
  • 64
  • +1 anyway there is no need for non-capturing groups since they are inside group that OP will use, not before it. – Pshemo Mar 21 '13 at 14:44