0

I have a task which requires me to replace a pattern of characters terminated by a @ symbol with the sub-word immediately following it. So, given presymbol@postsymbol for example, the replacement would produce postsymbol.

I am currently using this pattern '/(^|\s)@([a-z0-9_]+)/' to do the replacement, but my testers gave me feedback that putting " in front of the @, causes the replacement to fail.

How can I fix this pattern to output properly. Something similar to '/(^|\s)(@|"@)([a-z0-9_]+)/' ?

Perception
  • 79,279
  • 19
  • 185
  • 195
Grmn
  • 542
  • 2
  • 9

1 Answers1

1
/(^|\s)(@|"@)([a-z0-9_]+)/

If you add a new match group, $2 becomes $3.

What you ought to do is simply making " optional:

...."?@.... 

Or use a non-capturing match group:

....(?:@|"@)....

Or possibly put it as another alternative into the first group:

....(^|\s|")....
mario
  • 144,265
  • 20
  • 237
  • 291
  • * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Mar 17 '13 at 23:03
  • Thanks for the link to Regex.. bookmarked that one :) went for this one `'/(^|\s)?@([a-z0-9_]+)(?)/'` – Grmn Mar 17 '13 at 23:10