python 替换没有空格的正则表达式匹配
python replace regex match without spaces
我基本上想要 'join' 应该清楚地放在一起的数字。我想用自己替换正则表达式匹配但没有任何空格。
我有:
df
a
'Fraxiparine 9 500 IU (anti-Xa)/1 ml'
'Colobreathe 1 662 500 IU inhalačný prášok v tvrdej kapsule'
我想要:
df
a
'Fraxiparine 9500 IU (anti-Xa)/1 ml'
'Colobreathe 1662500 IU inhalačný prášok v tvrdej kapsule'
我正在使用 r'\d+\s+\d+\s*\d+'
来匹配数字,并且我创建了以下函数来删除字符串中的空格:
def spaces(x):
match = re.findall(r'\d+\s+\d+\s*\d+', x)
return match.replace(" ","")
现在我无法将该函数应用于完整数据框,但我也不知道如何用没有任何空格的字符串替换原始匹配项。
尝试使用以下代码:
def spaces(s):
return re.sub('(?<=\d) (?=\d)', '', s)
df['a'] = df['a'].apply(spaces)
正则表达式将匹配:
- 任意space
- 前面有一个数字
(?<=\d)
- 后跟一个数字
(?=\d)
。
然后,pandas.Series.apply 函数会将您的函数应用于数据框的所有行。
输出:
0 Fraxiparine 9500 IU (anti-Xa)/1 ml
1 Colobreathe 1662500 IU inhalačný prášok v tvrd...
我相信您的问题可以通过稍微调整您的函数来解决,以便应用于整个字符串 'match',如下所示:
import pandas as pd
import re
df = pd.DataFrame({'a' : ['Fraxiparine 9 500 IU (anti-Xa)/1 ml','Colobreathe 1 662 500 IU inhalačný prášok v tvrdej kapsule']})
# your function
def spaces(x):
match = re.findall(r'\d+\s+\d+\s*\d+', x)
replace_with = match[0].replace(" ","")
return x.replace(match[0], replace_with)
# now apply it on the whole dataframe, row per row
df['a'] = df['a'].apply(lambda x: spaces(x))
使用
df['a'] = df['a'].str.replace(r'(?<=\d)\s+(?=\d)', '', regex=True)
解释
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of look-ahead
如果您的计划是仅删除 \d+\s+\d+\s*\d+
中的空格:
df['a'] = df['a'].str.replace(r'\d+\s+\d+\s*\d+', lambda m: re.sub(r'\s+', '', m.group()), regex=True)
参见str.replace
:
repl : str or callable
Replacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used. See re.sub().
我基本上想要 'join' 应该清楚地放在一起的数字。我想用自己替换正则表达式匹配但没有任何空格。
我有:
df
a
'Fraxiparine 9 500 IU (anti-Xa)/1 ml'
'Colobreathe 1 662 500 IU inhalačný prášok v tvrdej kapsule'
我想要:
df
a
'Fraxiparine 9500 IU (anti-Xa)/1 ml'
'Colobreathe 1662500 IU inhalačný prášok v tvrdej kapsule'
我正在使用 r'\d+\s+\d+\s*\d+'
来匹配数字,并且我创建了以下函数来删除字符串中的空格:
def spaces(x):
match = re.findall(r'\d+\s+\d+\s*\d+', x)
return match.replace(" ","")
现在我无法将该函数应用于完整数据框,但我也不知道如何用没有任何空格的字符串替换原始匹配项。
尝试使用以下代码:
def spaces(s):
return re.sub('(?<=\d) (?=\d)', '', s)
df['a'] = df['a'].apply(spaces)
正则表达式将匹配:
- 任意space
- 前面有一个数字
(?<=\d)
- 后跟一个数字
(?=\d)
。
然后,pandas.Series.apply 函数会将您的函数应用于数据框的所有行。
输出:
0 Fraxiparine 9500 IU (anti-Xa)/1 ml
1 Colobreathe 1662500 IU inhalačný prášok v tvrd...
我相信您的问题可以通过稍微调整您的函数来解决,以便应用于整个字符串 'match',如下所示:
import pandas as pd
import re
df = pd.DataFrame({'a' : ['Fraxiparine 9 500 IU (anti-Xa)/1 ml','Colobreathe 1 662 500 IU inhalačný prášok v tvrdej kapsule']})
# your function
def spaces(x):
match = re.findall(r'\d+\s+\d+\s*\d+', x)
replace_with = match[0].replace(" ","")
return x.replace(match[0], replace_with)
# now apply it on the whole dataframe, row per row
df['a'] = df['a'].apply(lambda x: spaces(x))
使用
df['a'] = df['a'].str.replace(r'(?<=\d)\s+(?=\d)', '', regex=True)
解释
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of look-ahead
如果您的计划是仅删除 \d+\s+\d+\s*\d+
中的空格:
df['a'] = df['a'].str.replace(r'\d+\s+\d+\s*\d+', lambda m: re.sub(r'\s+', '', m.group()), regex=True)
参见str.replace
:
repl : str or callable
Replacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used. See re.sub().