How to negate regex validation string?

133 Views Asked by At

I want to replace all the string except the @[anyword]

I have string like this:

yng nnti dkasih tau :)"@mazayalinda: Yg klo ada cenel busana muslim aku mau ikutan dong "@noviwahyu10: Model ! Pasti gk blh klo k

and the @mazayalinda and @noviwahyu10 matches my regex @\w*.

However, I need to get rid all of the string, except for those 2 words above. We need to do the negation, but I am confuses about combining 2 regex, which are the regex to match the @[anyword] and the one to get rid all of the sentence except those 2 words.

Any ideas?

5

There are 5 best solutions below

3
On

It's not completely clear from the context if this is a viable solution, but when you want to replace everything except a certain pattern it sounds more like you want a regex search rather than a replacement. For example, in python it might look like:

>>> import re
>>> s = 'yng nnti dkasih tau :)"@mazayalinda: Yg klo ada cenel busana muslim aku mau ikutan dong "@noviwahyu10: Model ! Pasti gk blh klo k'
>>> re.findall(r'@\w+', s)
['@mazayalinda', '@noviwahyu10']

Edit: in js, something like this would be more appropriate:

var s = 'yng nnti dkasih tau :)"@mazayalinda: Yg klo ada cenel busana muslim aku mau ikutan dong "@noviwahyu10: Model ! Pasti gk blh klo k';

// code from http://www.activestate.com/blog/2008/04/javascript-refindall-workalike
var rx = new RegExp("@\\w+", "g");
var matches = new Array();
while((match = rx.exec(s)) !== null){
    matches.push(match);
}

After this, matches contains all the matched strings. You can always join it back together if needed into a single string.

3
On

You can use zero-width negative assertion, i.e., @(?!mazayalinda|noviwahyu10)\w.

This requires some sophisticated regular expression engine like Perl, Ruby, Java and so on. If you have classic engine, the way as @pcalcao sais is best.

0
On

Well, I'm not entirely sure I understand the question, but if you want to keep just the names and use the rest just "to get rid of it", why don't you simply save the names and ignore the rest ?

If you're keen on matching the "non-name pattern" - this seems to be an excerpt from some kind of conversation, where every message starts with ':'. If so, then using this should simply do the trick.

:[^@]*

6
On

It seems to me that you want to use capturing groups, not exactly negate the rest of the string, a regex like:

[^@]*(@\w+)[^@]*

Will capture those entries in capturing groups, and then, depending on your language, you can access each of the captured strings: http://rubular.com/r/qHKb35OK3g

9
On

use this regex (?<=^|@\w+\b)[^@]+ and union matches