且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用正则表达式匹配到某个模式

更新时间:2021-12-20 17:41:42

不使用捕获组,您可以使用前瞻((?= ... ) 业务).

Without using capture groups, you can use lookahead (the (?= ... ) business).

java\s?[^A-Z]*(?=\.[A-Z]) 应该可以捕获您想要的所有内容.分解如下:

java\s?[^A-Z]*(?=\.[A-Z]) should capture everything you're after. Here it is broken down:

java            //Literal word "java"
\s?             //Match for an optional space character. (can change to \s* if there can be multiple)
[^A-Z]*         //Any number of non-capital-letter characters
(?=\.[A-Z])     //Look ahead for (but don't add to selection) a literal period and a capital letter.