0

I have the following data line: oauthscope: command_path.use

Trying to create a RegEx expression that will retrieve just command_path.use

I've tried this, but it's selecting the opposite of what I want:

^oauthscope: (.*?)

My java code looks like this:

Pattern pattern = Pattern.compile("^oauthscope: (.*?)");
String mydata = "oauthscope: command_path.use";

Matcher matcher = pattern.matcher(mydata);
if(matcher.find()) {
    System.out.println("I found: '" + matcher.group(1) + "'");
} else {
    System.out.println("No match found!");
}

The result I get is this: I found: '' - I don't know how to tell it to fetch everything starting with that pattern to the end of the line.

JamesD
  • 679
  • 10
  • 36
  • Use `.*` to match any 0+ chars rather than a reluctant `.*?`. At the end of a pattern, `*?` quantified patterns never match any char, they do not have to consume chars. – Wiktor Stribiżew Oct 16 '18 at 18:09
  • It worked, but I don't understand why... Is removing the ? causing it to be more greedy in its matching? – JamesD Oct 16 '18 at 18:13
  • It just makes it try at least once. `.*?` does not trigger at all, it is only triggered when the *subsequent* subpatterns do not match. – Wiktor Stribiżew Oct 16 '18 at 18:33

0 Answers0