Python使用详细正则表达式解析用户输入

2024-06-26 08:16:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设计一个正则表达式来解析用户输入,以完整句子的形式。我在努力使我的表情充分发挥作用。我知道它没有很好的编码,但我正在努力学习。我目前正试图让它解析为一个字符串前见下面的代码。你知道吗

我的测试“句子”=How I'm 15.5% wholesome-looking U.S.A. we RADAR () [] {} you -- are, ... you?

text = input("please type somewhat coherently: ")

pattern = r'''(?x)              # set flag to allow verbose regexps
    (?:[A-Z]\.)+                # abbreviations, e.g. U.S.A.
    |\w+(?:[-']\w+)*            # permit word-internal hyphens and apostrophes
    |[-.(]+                     # double hyphen, ellipsis, and open parenthesis
    |\S\w*                       # any sequence of word characters
    # |[\d+(\.\d+)?%]           # percentages, 82%
    |[][\{\}.,;"'?():-_`]       # these are separate tokens
    '''

parsed = re.findall(pattern, text)
print(parsed)

我的输出=['How', "I'm", '15', '.', '5', '%', 'wholesome-looking', 'U.S.A.', 'we', 'RADAR', '(', ')', '[', ']', '{', '}', 'you', '--', 'are', ',', '...', 'you', '?']

我希望将'15', '.', '5', '%'解析为'15.5%'。当前被注释掉的行是应该做什么,但是当被注释掉的时候就什么也不做了。我寻找资源来帮助他们,但他们没有。你知道吗

谢谢你抽出时间。你知道吗


Tags: andtext用户youparsedare形式句子
1条回答
网友
1楼 · 发布于 2024-06-26 08:16:14

如果您只想将百分比作为一个整体进行匹配,那么您真的应该知道regex引擎从左到右分析输入字符串和模式。如果您有一个备选方案,将选择与输入字符串匹配的最左边的备选方案,其余的甚至不会被测试。你知道吗

因此,您需要向上拉可选的\d+(?:\.\d+)?,并且捕获组应该变成非捕获组,否则findall将产生奇怪的结果:

(?x)              # set flag to allow verbose regexps
(?:[A-Z]\.)+                # abbreviations, e.g. U.S.A.
|\d+(?:\.\d+)?%           # percentages, 82%  <  PULLED UP OVER HERE
|\w+(?:[-']\w+)*            # permit word-internal hyphens and apostrophes
|[-.(]+                     # double hyphen, ellipsis, and open parenthesis
|\S\w*                       # any sequence of word characters#
|[][{}.,;"'?():_`-]       # these are separate tokens

regex demo。你知道吗

另外,请注意,我用[][{}.,;"'?():_`-]替换了[][\{\}.,;"'?():-_`]:大括号不必转义,并且-从冒号(十进制代码58)和下划线(十进制95)形成了一个不必要的范围,匹配;<=>?@、所有大写拉丁字母、[\]^。你知道吗

相关问题 更多 >