0

I am looking for a regular expression that will extract the first word containing the character "@" on a given string. Additionally, it must not return the character "+" that prefixes words (except the first).

1) If I have the following string:

test@example.com +555 +blabla +@gmail.com

It must return: test@example.com

2) And if I have this one:

555 +@gmail.com +test@example.com

I want to get: @gmail.com

What I tried was \b.*@.*\b

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Jonathan P
  • 31
  • 5
  • 1
    Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](http://stackoverflow.com/help/how-to-ask), and where you are stuck. This will also help us answer your question better. – Thomas Ayoub Feb 01 '17 at 17:10
  • (?:^|\s)((?:[^+]\S*@\S*|(?<!\+)@\S*)) – maraaaaaaaa Feb 01 '17 at 17:19
  • @ThomasAyoub, sorry, you're right I forgot to tell what I tried before. I edited my question. Thanks for pointing me in the good direction on how to ask good question here. That is only my second question ever on Stack Overflow. – Jonathan P Feb 01 '17 at 17:53
  • What is the regex flavor? Try `[^\s+@]*@\S*` – Wiktor Stribiżew Feb 01 '17 at 18:24
  • @EJoshuaS, thanks, it is working. – Jonathan P Feb 01 '17 at 18:25
  • @WiktorStribiżew, I am using .NET. – Jonathan P Feb 01 '17 at 18:27
  • Excuse me, but what pattern is working? Is that C# or VB.NET? Or anything else? – Wiktor Stribiżew Feb 01 '17 at 18:30
  • @WiktorStribiżew, the pattern that maksymiuk gave here is working as expected. I am using VB.NET but I think that the .NET language has no impact on the regex. – Jonathan P Feb 01 '17 at 18:39
  • [It does not work with the second string.](http://regexstorm.net/tester?p=%28%3f%3a%5e%7c%5cs%29%28%28%3f%3a%5b%5e%2b%5d%5cS*%40%5cS*%7c%28%3f%3c!%5c%2b%29%40%5cS*%29%29&i=555+%2b%40gmail.com+%2btest%40example.com) – Wiktor Stribiżew Feb 01 '17 at 19:37

0 Answers0