I've managed to get everything (well, all letters) up to a whitespace using the following:
@"^.*([A-Z][a-z].*)]\s"
However, I want to to match to a (
instead of a whitespace... how can I manage this?
Without having the '(' in the match
If what you want is to match any character up until the (
character, then this should work:
@"^.*?(?=\()"
If you want all letters, then this should do the trick:
@"^[a-zA-Z]*(?=\()"
Explanation:
^ Matches the beginning of the string
.*? One or more of any character. The trailing ? means 'non-greedy',
which means the minimum characters that match, rather than the maximum
(?= This means 'zero-width positive lookahead assertion'. That means that the
containing expression won't be included in the match.
\( Escapes the ( character (since it has special meaning in regular
expressions)
) Closes off the lookahead
[a-zA-Z]*? Zero or more of any character from a to z, or from A to Z
Reference: Regular Expression Language - Quick Reference (MSDN)
EDIT: Actually, instead of using .*?
, as Casimir has noted in his answer it's probably easier to use [^\)]*
. The ^
used inside a character class (a character class is the [...]
construct) inverts the meaning, so instead of "any of these characters", it means "any except these characters". So the expression using that construct would be:
@"^[^\(]*(?=\()"
Using a constraining character class is the best way
@"^[^(]*"
[^(]
means all characters but (
Note that you don't need a capture group since that you want is the whole pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With