Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I match everything after @ until space?

I have this string:

var str = 'این یک @پیا.م تست است';
// I want this     ^^^^^

I can select it like this:

/@(.{5})/

But it isn't what I need, because the length of that word which is after @ and before space isn't always 5. I really don't know why \w doesn't matches Persian characters. Or even [a-zA-Z] doesn't work either.

Well, how can I do that?

like image 373
stack Avatar asked Feb 21 '16 20:02

stack


People also ask

How do I match a character except space in regex?

You can match a space character with just the space character; [^ ] matches anything but a space character.

What is used to match anything except a whitespace?

What is used to match anything except a whitespace? The complement, \S , matches any non-whitespace character.

How do you match everything after a word in regex?

If you want . to match really everything, including newlines, you need to enable "dot-matches-all" mode in your regex engine of choice (for example, add re. DOTALL flag in Python, or /s in PCRE.

How do you match a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.


2 Answers

As a Unicocde independent approach you can simply use a negated character class :

'@([^ ]+)'

See demo https://regex101.com/r/oD9hV0/1

like image 185
Mazdak Avatar answered Sep 28 '22 18:09

Mazdak


You could use the follwing regex That will return anything beteen @ and fot . :

@(.*?)[\s]

@ : matches the character @ literally

(.*?) : matches any character (except newline)

\s : match any white space character [\r\n\t\f ]

Hope this helps.

like image 28
Zakaria Acharki Avatar answered Sep 28 '22 16:09

Zakaria Acharki