0

I come from here Regex: match everything but But they only say how to get a Regex containing all but some string.

What im trying to do is finding a regular expression that matches me all the text but this ^([\S+" "?]+" "@[a-zA-Z0-9_]{1,15})$ (or any other regex).

Guillem
  • 144
  • 4
  • 13
  • Could you provide an example of some text, and the text you are hoping to retrieve from it? Also, the ^ and $ match the start and end of the string respectively so the expression you specified will not match if there are leading or trailing characters. – Lucy Stevens Feb 09 '18 at 13:08
  • I hope to match from this text: "User Name @username Text Text Text Text random @thing" This: "Text Text Text Text random @thing" – Guillem Feb 09 '18 at 13:27

1 Answers1

0

The following regex will match words separated by spaces before a word starting with a @ character;

([^@\s]+ )+@[\w]{1,15}

For example, this will return two matches for the String:

User Name @username Text Text Text Text random @thing

  • User Name @username
  • Text Text Text Text random @thing

You can then select the second match to get the text you need. Alternatively, if you know what @thing will be before compiling the regex (e.g. @response, @config, etc.) you can replace the @[\w]{1,15} section of the regex with the specific @thing to only retrieve the text you want.

Lucy Stevens
  • 185
  • 2
  • 9
  • The problem is that the text can have something that starts with "@" or not. – Guillem Feb 09 '18 at 15:31
  • @Koalapa you need some way of determining where the text ends, whether that be a string starting with `@` or the end of the string. Is there some hard rule for where the text ends? Or how much text should be retrieved? – Lucy Stevens Feb 09 '18 at 15:33
  • The text ends when it exceeds 140 chars afther that first @username. – Guillem Feb 09 '18 at 15:40
  • But I guess the regex you gave me does the work. Thanks you very much – Guillem Feb 09 '18 at 15:49
  • @Koalapa Depending on what language you're using you may be able to use this; `(?<=@username).{1,140}` which matches the first 140 characters after the string `@username` but requires the regex engine to support lookbehind (which javascript does not). – Lucy Stevens Feb 09 '18 at 15:50