Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

regex - Regular Expression Pattern K Alternatives in C#

I have a regex expression that I tested on http://gskinner.com/RegExr/ and it worked, but when I used it in my C# application it failed.

My regex expression: (?<!d)d{6}Kd+(?=d{4}(?!d)) Text: 4000751111115425 Result: 111111

What is wrong with my regex expression?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This issue you are having is that .NET regular expressions do not support K, "discard what has been matched so far".

I believe your regex translates as "match any string of more than ten d digits, to as many digits as possible, and discard the first 6 and the last 4".

I believe that the .NET-compliant regex

(?<=d{6})d+(?=d{4})

achieves the same thing. Note that the negative lookahead/behind for no-more-ds is not necessary as the d+ is greedy - the engine already will try to match as many digits as possible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...