1

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?

deadly
  • 1,194
  • 14
  • 24
nencor
  • 1,621
  • 3
  • 14
  • 15

5 Answers5

2

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.

FatalError
  • 52,695
  • 14
  • 99
  • 116
  • i am using regexpal.com to test my regex, sorry :( – nencor Apr 29 '13 at 14:22
  • @nencor: Is javascript regex your ultimate goal? Doing a search like this is certainly possible in js, though it does not look like that interface provides a way to test it. – FatalError Apr 29 '13 at 14:24
  • yes perhaps, i need to get rid all of the string, so i can get the username and use them in yahoopipes :( – nencor Apr 29 '13 at 14:29
0

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

pcalcao
  • 15,789
  • 1
  • 44
  • 64
  • I'm not sure if it's incorrect or not, if the user wants to get tweet handles, @@# isn't a valid one. Either way, it's just a matter of swapping `\w` for whatever needs to be in there. – pcalcao Apr 29 '13 at 14:14
  • The example is to show that, when the input contains garbage such as `@@#`, your regex will fail to remove the part in front. – nhahtdh Apr 29 '13 at 14:15
  • that regex will select the entire string, i need to select !(@username) :( – nencor Apr 29 '13 at 14:16
  • @nencor: This is a trick so that you will retain the `@username` part in the resulting string. Otherwise, it will be hard (impossible?) to write a regex that match only the part without `@username` – nhahtdh Apr 29 '13 at 14:20
  • yeah, its difficult to get rid all the string except the string which begins with @ – nencor Apr 29 '13 at 14:26
  • My regex obtains the part you need, using capturing groups, it doesn't eliminate anything. It's a better approach, and you can use them in most languages, if not all. – pcalcao Apr 29 '13 at 14:34
0

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

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
0

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.

:[^@]*

Melchior
  • 100
  • 1
  • 7
-1

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.

habe
  • 714
  • 5
  • 5