Match the string until it encounters '('

I managed to get everything (well, all letters) to a space using the following:

@"^.*([AZ][az].*)]\s" 

However, I want to combine with ( instead of a space ... how can I do this?

Without the presence of '(' in the match

+4
source share
3 answers

If you want to match any character to a character ( then this should work:

 @"^.*?(?=\()" 

If you need all the 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 

Link: Regular Expression Language - Short Link (MSDN)

EDIT : Actually, instead of using .*? as Casimir noted in his answer, it is easier to use [^\)]* . ^ , used inside a character class (a character class is a construction [...] ), inverts the value, so instead of "any of these characters" it means "any other than these characters." Thus, an expression using this construct will be:

 @"^[^\(]*(?=\()" 
+9
source

Using a bounding character class is the best way

 @"^[^(]*" 

[^(] means all characters, but (

Note that you do not need a capture group, as you need the entire template.

+3
source

You can use this template:

 ([AZ][az][^(]*)\( 

The group will correspond to the main Latin letter, followed by the lowercase Latin letter, followed by any number of characters except the open parenthesis. Please note that ^.* not required.

Or this, which creates the same basic behavior, but uses a non-greasy quantifier instead:

 ([AZ][az].*?)\( 
0
source

Source: https://habr.com/ru/post/1488903/


All Articles