I my trying to write regex on my flutter application. I have this text:
@rtt tty tr@fh @rggg
I want to just split @rtt , @rggg so I add this regex pattern: ^@(\S ).
But this pattern just split first matches: @rtt. https://regex101.com/r/p2K9Yy/1
How can I write a pattern to split all matches ?
CodePudding user response:
You can use
(?<!\S)@(\S )
\B@(\S )
The (?<!\S) negative lookbehind requires either start of string or a whitespace immediately to the left of the current location, and \B before @ requires either start of string or a non-word char to appear immediately before.
See the regex demo #1 and regex demo #2.
In Dart, you can use
String x ="@rtt tty tr@fh @rggg";
RegExp rx = new RegExp(r'\B@(\S )');
var values = rx.allMatches(x).map((z) => z.group(1)).toList();
print(values); // => [rtt, rggg]
var values2 = rx.allMatches(x).map((z) => z.group(0)).toList();
print(values2); // => [@rtt, @rggg]
