1

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 ?

0

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.