gpt4 book ai didi

php - 正则表达式替换混合数字+字符串

转载 作者:可可西里 更新时间:2023-10-31 23:57:13 24 4
gpt4 key购买 nike

我想删除所有包含数字的单词,例子:

LW23 London W98 String

在上面的字符串中,我唯一想保留的是"London String"。这可以用正则表达式来完成吗?

我目前使用的是 Python,但 PHP 代码也很好。

谢谢!

编辑:

这是我现在能做的:

>>> a = "LW23 London W98 String"
>>> b = a.split(' ')
>>> a
['LW23', 'London', 'W98', 'String']

最佳答案

是的,你可以:

result = re.sub(
r"""(?x) # verbose regex
\b # Start of word
(?= # Look ahead to ensure that this word contains...
\w* # (after any number of alphanumeric characters)
\d # ...at least one digit.
) # End of lookahead
\w+ # Match the alphanumeric word
\s* # Match any following whitespace""",
"", subject)

关于php - 正则表达式替换混合数字+字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13453999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com