本文介绍了使用正则表达式匹配到某个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个包含一些文本的文本文件中有字符串,如下所示:
I have string in a text file containing some text as follows:
txt = "java.awt.GridBagLayout.layoutContainer"
我希望获得类名之前的所有内容,"GridBagLayout"
.
I am looking to get everything before the Class Name, "GridBagLayout"
.
我尝试了以下内容,但我不知道如何摆脱 "."
I have tried something the following , but I can't figure out how to get rid of the "."
txt = re.findall(r'java\S?[^A-Z]*', txt)
我得到以下信息:"java.awt."
而不是我想要的:"java.awt"
关于我如何解决这个问题的任何指示?
Any pointers as to how I could fix this?
推荐答案
不使用捕获组,您可以使用前瞻((?= ... )
业务).
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.
这篇关于使用正则表达式匹配到某个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!