1
var userQuestion = '10 5-√x 96';
2
var userQuestion = '10 5-√x 96-√x-2 √x';
i am expecting to get the value between square root and the next operator in the string and grouping them into different strings
how can i slice the string √x if i don't know it's index in dart.
and make other strings with respect to the number of √x in the string
eg in 1 i want a string slice that will create
var squareRoot = '√x';
and in 2 i want to get something like this
var squareRoot = '√x';
var squareRoot1 = '√x';
var squareRoot2 = '√x';
CodePudding user response:
If you want all the squareroots including whatever comes after, until you hit an operator, then this should do the trick:
var userQuestion = '10 5-√x 96-√y-2 √5/√123*√256';
var regexp = RegExp(r'(√[\w\d] )[ \-*/]?');
var matches = regexp.allMatches(userQuestion);
var squareRoots = matches.map((e) => e.group(1)).toList();
print(squareRoots);
Will print:
[√x, √y, √5, √123, √256]
There is obviously no way to declare variables with names as you did if it is an unknown number of square roots, so you'll have to use a List<String>
that hold your strings, as I did above.