-1

I am trying to save the bolded text to a string.
I have come up with the regex "routeName?.+?routeLengthKM" and according to an online regex tester this should get me what I want with the find method but it is only returning true/false. How can i save the bolded text to a string?

"routes:[{routeName:Dulles Toll Rd W; SR-28 S,routeDurationInMinutes:18,routeLengthKM:21.474,routeLengthMiles:13.343320854,toll:true},{routeName:Frying Pan Rd; SR-28 S,routeDurationInMinutes:18,routeLengthKM:19.437,routeLengthMiles:12.077588127,toll:false}

package regex;
import java.util.regex.*;

public class regexclass {

  public static void main (String args[]){
    Pattern p= Pattern.compile("routeName?.+?routeLengthKM");
    Matcher m= p.matcher("routes:[{routeName:Dulles Toll Rd W; SR-28 S,routeDurationInMinutes:18,routeLengthKM:21.474,routeLengthMiles:13.343320854,toll:true},{routeName:Frying Pan Rd; SR-28 S,routeDurationInMinutes:18,routeLengthKM:19.437,routeLengthMiles:12.077588127,toll:false}]");
    System.out.println(m.find());

  }
}
depperm
  • 10,606
  • 4
  • 43
  • 67
tylerik
  • 97
  • 2
  • 10

1 Answers1

0

m.find(); will launch the matching engine. This will try to match the regex against the text. It returns true if a match could be found, and false if no match could be found. Normally, you would use a construct like this to find all matches:

while(m.find()) {}

After having found a match (i.e. after m.find(); or better if (m.find()) {), you can retrieve the match with m.group(n); with n the group number. 0 is the full match group and all other numbers up from there (1,2,3,...) refer to subgroups (things in parentheses).

blueygh2
  • 1,538
  • 10
  • 15