1

I am having trouble writing a regular expression in C#; its purpose is to extract all words that start with '@' from a given string so they can be stored in some type of data structure.

If the string is "The quick @brown fox jumps over the lazy @dog", I'd like to get an array that contains two elements: brown and dog. It needs to handle the edge cases properly. For example, if it's @@brown, it should still produce 'brown' not '@brown'.

7
  • word will be any alphanumeric string with no spaces.
    – Prabhu
    May 11, 2011 at 18:40
  • @Prabhu: yes, but are niño, éléphant or Marie-Hélène words ?
    – Toto
    May 11, 2011 at 18:45
  • Is an alphanumeric string meaning one that contains nothing but \p{Alphabetic} and/or \p{Number} code points in it? Or did you have some more restricted meaning that you wished to impose?
    – tchrist
    May 11, 2011 at 18:45
  • @M42, a word can be: @"^([A-Za-z0-9])*"
    – Prabhu
    May 11, 2011 at 18:51
  • If they are words, then you cannot use \w. You must use the true \p{Alphabetic} property with additional stipulations.
    – tchrist
    May 11, 2011 at 18:52

3 Answers 3

3

something like this

C#:

string quick = "The quick @brown fox jumps over the lazy @dog @@dog";
MatchCollection results = Regex.Matches(quick, "@\\w+");

foreach (Match m in results)
{
    Literal1.Text += m.Value.Replace("@", "");
}

takes care of your edge case too. (@@dog => dog)

10
  • Does \w in C♯ meet The Unicode Standard’s requirement that \w must match exactly [\p{Alphabetic}\p{Decimal_Number}\p{Mark}\p{Connector_Punctuation}]? If so, it matches both more and less than the original request. If it it does not, then it surely matches far less than the original request specified.
    – tchrist
    May 11, 2011 at 18:48
  • \w matches ` [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}], whilst (natch!) \W` matches its negation and \b (word boundary) matches a zero-width (^|$|\W). May 11, 2011 at 18:52
  • Matches: Lowercase, Uppercase, Titlecase, Other, Modifier, Number/Decimal Digit, Punctuation Connector (such as _) Word Character: \w - MSDN
    – MikeM
    May 11, 2011 at 18:55
  • @Nicholas: So they still haven’t made it match what Unicode says it must. That’s too bad. It is missing the \p{Other_Alphabetic} characters, which includes various \p{GC=Mark}, \p{GC=Letter_Number}, and \p{GC=Symbol} code points. Check the spec.
    – tchrist
    May 11, 2011 at 18:56
  • 1
    @tchrist: Whatever. This appears to be an issue that pushes your buttons (google.com/…). Enjoy. May 11, 2011 at 23:01
3

@[\w\d]+ should work for you.

Tested using http://www.regextester.com/.

This works by matching for the @, followed by one or more word characters. The \w represents any "word character" (character sets), the \d represents any digit, and the + (repetition) indicates one or more. The \w and \d are both allowed by being wrapped in brackets.

To exclude the @ you could use str.Substring(1) to ignore the first character, or use the regex @([\w\d]+) and extract the first group.

5
  • 1
    I do not believe there exists any definition of \w that maps to alphanumeric strings. Neither the stupid and useless old ASCII definition that some regex libraries are still 20 years out of date with, nor the current version required by The Unicode Standard maps exactly to [\p{Alphabetic}\p{Number}].
    – tchrist
    May 11, 2011 at 18:50
  • @tchrist He hadn't specified alpha-numeric when I posted.
    – C. Ross
    May 11, 2011 at 18:58
  • @tchrist The tester does map it that way.
    – C. Ross
    May 11, 2011 at 19:00
  • Ah, I see. That makes sense then. English is generally far less precise than our regexes demand.
    – tchrist
    May 11, 2011 at 19:00
  • Whether maps it that way or not is immaterial. What matters is whether or not it is compliant with The Unicode Standard. It seems that it is not a conforming application.
    – tchrist
    May 11, 2011 at 19:02
0

Depending on your definition of "word" (\w is more the C-language definition of a symbol valid in an identifier or keyword: [a-z0-9_].), you might try the folowing — I'm defining "word" here as a sequence of non-whitespace characters:

(^|\s)(@+(?<atword>[^\s]+))(\s|$)

The above has been tested here, and matches the following:

  • Match start-of-string or a whitespace character, followed by
  • 1 or more @ characters, followed by
  • 1 or more non-whitespace characters, in group named 'atword', followed by
  • a whitespace character or end-of-string.

For successful matches, the named group atword will contain the text following the lead-in @ sign(s).

So:

  • This @@ foo won't match.
  • This @foo bar will match
  • `@@@foobarbat is kind of silly will match
  • `@@@foobar@bazabat will match.
  • silly.@rabbit, tricks are for kids won't match, but
  • silly @rabbit, tricks are for kids will match and you'll get rabbit, rather than rabbit (like I said, you need to think about how you define 'word'.
  • etc.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.