5

I'm trying to use regex to find a particular starting character and then getting the rest of the text on that particular line.

For example the text can be like ...

V: mid-voice T: tempo

I want to use regex to grab "V:" and the the text behind it.

Is there any good, quick way to do this using regular expressions?

user941401
  • 323
  • 1
  • 3
  • 8

3 Answers3

4

If your starting character were fixed, you would create a pattern like:

Pattern vToEndOfLine = Pattern.compile("(V:[^\\n]*)")

and use find() rather than matches().

If your starting character is dynamic, you can always write a method to return the desired pattern:

Pattern getTailOfLinePatternFor(String start) {
    return Pattern.compile("(" + start + "[^\\n]*");
}

These can be worked on a little bit depending on your needs.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
1

For a pattern match try for your example:

V:.*$

AJcodez
  • 31,780
  • 20
  • 84
  • 118
1

Here's the best, cleanest and easiest (ie one-line) way:

 String[] parts = str.split("(?<=^\\w+): ");

Explanation:

The regex uses a positive look behind to break on the ": " after the first word (in this case "V") and capture both halves.

Here's a test:

String str = "V: mid-voice T: tempo";
String[] parts = str.split("(?<=^\\w+): ");
System.out.println("key = " + parts[0] + "\nvalue = " + parts[1]);

Output:

key = V
value = mid-voice T: tempo
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Does this actually work? [As far as I know](http://www.regular-expressions.info/refflavors.html), Java doesn't support indefinite-length lookbehind assertions. – Tim Pietzcker Oct 24 '11 at 06:41
  • @TimPietzcker Does it work?? Yes, it works! It's a pretty basic look behind - it is locked to the start-of-line with the `^`, so it's only going to hit at most once. Copy-paste the test code into a main and run it with a few edge cases to see for yourself. – Bohemian Oct 24 '11 at 12:04
  • That's interesting. Does the Java regex engine support infinite length lookbehind assertions only if they're anchored like that, or does it do so generally, and the site I linked to above (and many answers on SO) are wrong and need to be updated? – Tim Pietzcker Oct 24 '11 at 12:11
  • 1
    @TimPietzcker I did some research and wrote tests to check... you are correct - trying to use an infinite lookbehind in java produces: `PatternSyntaxException: Look-behind group does not have an obvious maximum length`. That said, this lookbehind is OK (no error). Further, removing the anchor to leave just `"(?<=\\w+): "` is *still* OK, even though technically it is infinite length. Through testing, I've concluded that for java the disallowed lookbehinds use regexes that have constant followed by an "any length" match: ie `(?<=x[\w]*)` is **not** OK, `(?<=[\w]*x)` **is** OK. Interesting! – Bohemian Oct 25 '11 at 00:10
  • I've found a question on SO that explains why this is so: http://stackoverflow.com/questions/1536915/regex-look-behind-without-obvious-maximum-length-in-java – Tim Pietzcker Oct 25 '11 at 06:19