Assuming that you mean to match *
literally as an asterisk, then you need to escape it with a preceding \
as follows.
@"^User \* has (logged out|joined)$"
If by *
you mean "any username", then substitute it with either the regex you use to validate your usernames, or for lack of anything better, you can always use just .*
.
The (logged out|joined)
construct is called an "alternation". The surrounding parentheses, in addition to enforcing precedence on the alternation in this case, also captures into group 1 either the string "logged out"
or "joined"
, depending on which alternate matches.
If you don't need to distinguish between the two events, then you don't really need this string captured, and you can use a non-capturing group (?:logged out|joined)
, for slight performance gain.
The ^
and $
are what are called anchors, which respectively match the beginning and end of the string. Proper placement of these anchors in the pattern ensures that it matches the entirety of the input string, not just a substring.
regular-expressions.info Links