skip to Main Content

I have a string which may contain quoted text, something like this
String question = 'Vertices of the triangle are "(5,-2),(-1,2)(1,4)"'
I want to get that quoted text from the string and store it in another string like this.
String numbers = '(5,-2),(-1,2),(1,4)'

I have tried finding a solution and I have tried to read the documentation on string but couldn’t find a solution.

2

Answers


  1. You can do it with a regular expression:

    final String question = 'Vertices of the triangle are "(5,-2),(-1,2)(1,4)"';
    final match = RegExp(r'"((?:\.|[^"\])*)"').firstMatch(question);
    print(match?.group(1));
    
    Login or Signup to reply.
  2. String question =
          'Vertices of the triangle are "(5,-2),(-1,2)(1,4)"';
    
      RegExp regExp = RegExp(r'"(.*?)"');
      List<RegExpMatch> matches = regExp.allMatches(question).toList();
    
      String numbers = matches.map((match) => match.group(1)).join(',');
      print(numbers); // Output: (5,-2),(-1,2),(1,4),(10,10),(15,20),(25,30)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search