Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression "(?<!^)(?=[A-Z])"

Tags:

c#

regex

I spent like three hours trying to understant how does "(?<!^)(?=[A-Z])" works to split at tring according to capital letters i.e.

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. why "(? < !^)([A-Z])" doesnot work ? it means that whenever you find a captial letter that is not after a new line, then split, am I right ?

like image 949
Emo Avatar asked Oct 23 '25 05:10

Emo


1 Answers

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 [A-Z] matchies any capital letter, and (?=...) is a positive look ahead, so (?=[A-Z]) matches any position in the string that is right before a capital letter.

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.

like image 144
Guffa Avatar answered Oct 24 '25 20:10

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!