简体   繁体   English

为什么我不能用这个python正则表达式摆脱L?

[英]Why can’t I get rid of the L with this python regular expression?

I'm trying to get rid of the Ls at the ends of integers with a regular expression in python: 我正在尝试使用python中的正则表达式摆脱整数结尾处的Ls:

import re
s = '3535L sadf ddsf df 23L 2323L'
s = re.sub(r'\w(\d+)L\w', '\1', s)

However, this regex doesn't even change the string. 但是,此正则表达式甚至不会更改字符串。 I've also tried s = re.sub(r'\\w\\d+(L)\\w', '', s) since I thought that maybe the L could be captured and deleted, but that didn't work either. 我还尝试过s = re.sub(r'\\w\\d+(L)\\w', '', s)因为我认为也许可以捕获和删除L,但这也不起作用。

\\w = [a-zA-Z0-9_] \\w = [a-zA-Z0-9_]


In other words, \\w does not include whitespace characters. 换句话说, \\w不包含空格字符。 Each L is at the end of the word and therefore doesn't have any "word characters" following it. 每个L都在单词的末尾,因此后面没有任何“单词字符”。 Perhaps you were looking for word boundaries ? 也许您正在寻找单词边界

re.sub(r'\b(\d+)L\b', '\1', s)

Demo 演示版

I'm not sure what you're trying to do with those \\w s in the first place, but to match a string of digits followed by an L , just use \\d+L , and to remove the L you just need to put the \\d+ part in a capture group so you can sub it for the whole thing: 我不确定您首先想对这些\\w做什么,但是要匹配一串数字和一个L ,只需使用\\d+L ,然后删除L即可将\\d+部分放在捕获组中,以便您可以将其作为整个子对象使用:

>>> s = '3535L sadf ddsf df 23L 2323L'
>>> re.sub(r'(\d+)L', r'\1', s)
'3535 sadf ddsf df 23 2323'

Here's the regex in action: 这是实际的正则表达式:

(\d+)L

正则表达式可视化

Debuggex Demo Debuggex演示

Of course this will also convert, eg, 123LBQ into 123BQ , but I don't see anything in your examples or in your description of the problem that indicates that this is possible, or which possible result you want for that, so… 当然,这也会将例如123LBQ转换为123BQ ,但是我在您的示例或问题描述中看不到任何表明这样做的可能或您想要的可能结果,所以……

You can use look behind assertion 您可以使用后置断言

>>> s = '3535L sadf ddsf df 23L 2323L'
>>> s = re.sub(r'\w(?<=\d)L\b', '', s)
>>> s
'353 sadf ddsf df 2 232'

(?<=\\d)L asserts that the L is presceded by a digit, in which case replace it with null '' (?<=\\d)L断言L以数字开头,在这种情况下用null替换''

Try this: re.sub(r'(?<=\\d)L', '\\1', s) 试试这个: re.sub(r'(?<=\\d)L', '\\1', s)

This uses a lookbehind to find a digit followed by an "L". 它使用后向查找来找到一个数字,后跟一个“ L”。

Why not use a - IMO more readable - generator expression ? 为什么不使用IMO更具可读性的generator expression

>>> s = '3535L sadf ddsf df 23L 2323L'
>>> ' '.join(x.rstrip('L') if x[-1:] =='L' and  x[:-1].isdigit() else x for x in s.split())
'3535 sadf ddsf df 23 2323'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM