Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match a string until it meets a '('

Tags:

c#

regex

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

like image 588
Theun Arbeider Avatar asked Dec 16 '22 09:12

Theun Arbeider


2 Answers

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:

@"^[^\(]*(?=\()"
like image 106
rossipedia Avatar answered Dec 25 '22 15:12

rossipedia


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.

like image 42
Casimir et Hippolyte Avatar answered Dec 25 '22 14:12

Casimir et Hippolyte