简体   繁体   English

正则表达式“(?<!^)(?= [A-Z])”

[英]regular expression “(?<!^)(?=[A-Z])”

I spent like three hours trying to understant how does "(?<!^)(?=[AZ])" works to split at tring according to capital letters ie 我花了三个小时试图理解"(?<!^)(?=[AZ])"如何按照大写字母进行分割,即

string[] s = Regex.Split("TheWorldWithoutStrangers", "(?<!^)(?=[A-Z])");

How does it work !! 它是如何工作的 !! I do understand what is the meaning of each char in the above expression, but I do not get how does it work together. 我明白上面表达式中每个char的含义是什么,但我不知道它是如何一起工作的。 why "(? < !^)([AZ])" doesnot work ? 为什么"(? < !^)([AZ])"不起作用? it means that whenever you find a captial letter that is not after a new line, then split, am I right ? 这意味着每当你找到一个不在新线之后的大写字母,然后拆分,我是对的吗?

The ^ means the beginning of a line, and (?<!...) is a negative look behind, so (?<!^) matches any position in the string that is not right at the beginning of the line. ^表示一行的开头, (?<!...)后面是负面外观,因此(?<!^)匹配字符串中不在该行开头的任何位置。

The set [AZ] matchies any capital letter, and (?=...) is a positive look ahead, so (?=[AZ]) matches any position in the string that is right before a capital letter. 集合[AZ]匹配任何大写字母,而(?=...)是正向前看,因此(?=[AZ])匹配大写字母前面的字符串中的任何位置。

Put them together, and the expression matches any position in the string that is not right at the beginning of a line, and that is right before a capital letter. 将它们放在一起,表达式匹配字符串中不在行开头的任何位置,这就在大写字母之前。

The key here is that the two parts (?<!...) and (?=...) are zero-width assertions. 这里的关键是两部分(?<!...)(?=...)是零宽度断言。 The first one makes sure ^ (start of string) does not occur right before the match position and the second one ensures that [AZ] (single capital letter) appears right after the match position. 第一个确保^ (字符串的开头)不在匹配位置之前发生,第二个确保[AZ] (单个大写字母)出现在匹配位置之后。 The actual match is empty because neither of the assertions actually match any characters. 实际匹配为空,因为这两个断言都没有实际匹配任何字符。 The entire expression merely matches a position. 整个表达式仅匹配一个位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM