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

new*_*v14 8 python regex

我在包含一些文本的文本文件中有字符串,如下所示:

txt = "java.awt.GridBagLayout.layoutContainer"
Run Code Online (Sandbox Code Playgroud)

我希望在课程名称之前获得所有内容"GridBagLayout".

我尝试过以下内容,但我无法弄清楚如何摆脱它 "."

txt = re.findall(r'java\S?[^A-Z]*', txt)
Run Code Online (Sandbox Code Playgroud)

我得到以下内容: "java.awt."

而不是我想要的: "java.awt"

关于如何解决这个问题的任何指示?

Nig*_*cat 17

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

java\s?[^A-Z]*(?=\.[A-Z])应该抓住你所追求的一切.这里分解了:

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.
Run Code Online (Sandbox Code Playgroud)