[英]Match a string until it meets a '('
我已經設法使用以下內容將所有內容(好吧,所有字母)都放到空白處:
@"^.*([A-Z][a-z].*)]\s"
但是,我想匹配一個(
而不是空格......我怎么能管理這個?
沒有'('在比賽中
如果你想要的是匹配任何角色,直到(
角色,那么這應該工作:
@"^.*?(?=\()"
如果你想要所有的字母,那么這應該是訣竅:
@"^[a-zA-Z]*(?=\()"
說明:
^ 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
編輯 :實際上,而不是使用.*?
正如Casimir在他的回答中指出的那樣,使用[^\\)]*
可能更容易。 在字符類中使用的^
(字符類是[...]
構造)反轉了含義,因此它代替“任何這些字符”,它意味着“ 除了這些字符之外的任何字符”。 所以使用該結構的表達式將是:
@"^[^\(]*(?=\()"
使用約束字符類是最好的方法
@"^[^(]*"
[^(]
表示所有字符但是(
請注意,您不需要捕獲組,因為您需要的是整個模式。
您可以使用此模式:
([A-Z][a-z][^(]*)\(
該組將匹配大寫拉丁字母,后跟小寫拉丁字母,后跟除開括號之外的任意數量的字符。 請注意, ^.*
不是必需的。
或者這個,它產生相同的基本行為,但使用非貪婪的量詞 :
([A-Z][a-z].*?)\(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.