如何用正则表达式进行模式替换?

马龙

我要在此字符串中匹配“ windowsXP”:

windowsXP|7|8 

或者

windowsXP 7 8

我写了一个模式来匹配它:

 ([^0-9|])+[0-9\.]+.*

为什么不匹配?是| 麻烦在支架上。我尝试将其转义,但还是没有用。

([^0-9\|])+[0-9\.]+.*
Ryszard捷克

看:

  1. ([^0-9|])+ 查找与数字和管道不同的字符
  2. [0-9\.]+ 尝试匹配数字或句点,但是表达式无法捕获到管道。

使用

^([^|\s]+)[|\s](\d+)[|\s](\d+)

证明

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^|\s]+                  any character except: '|', whitespace (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [|\s]                    any character of: '|', whitespace (\n, \r,
                           \t, \f, and " ")
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  [|\s]                    any character of: '|', whitespace (\n, \r,
                           \t, \f, and " ")
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \3

Python代码

import re
regex = r"^([^|\s]+)[|\s](\d+)[|\s](\d+)"
test_str = "windowsXP|7|8"
print(re.findall(regex, test_str))

结果[('windowsXP', '7', '8')]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何用正则表达式计算或替换javascript中{}或[]之间的所有匹配单词?

如何用正则表达式替换组的内容

如何用单个正则表达式模式替换过多的SQL通配符?

SED如何用正则表达式替换Line

如何用正则表达式替换文本?

如何用正则表达式替换WebStorm / PhpStorm

如何用正则表达式替换多个重叠模式?

如何用数字进行正则表达式替换-例如$ 1 <number>

如何用正则表达式替换多个匹配项/组?

正则表达式-如何测试2种str模式并根据匹配的str模式进行替换

如何用正则表达式设置多行模式

如何用|进行正则表达式?

如何用匹配的正则表达式模式替换特定位置的字符串

正则表达式如何在正则表达式上对整个模式正确进行正则表达式分割(问号等于)

如何用数据框中的数字替换正则表达式

如何用sed表达此正则表达式?

Perl正则表达式:如何用html标签替换终端控制字符?

如何用正则表达式模式替换文本并在替换文本中集成一个计数器?

如何用变量替换vim正则表达式

如何用正则表达式替换字符序列?

使用正则表达式进行模式替换

正则表达式多行模式和替换替换

如何使用正则表达式模式替换+91?

如何使用正则表达式替换模式?

如何用一个表达式替换正则表达式向量?

如何用正则表达式替换?

如何用标记(正则表达式)之间的部分替换字符串?

如何对相同的正则表达式模式 xxx 进行多次不同的替换,但替换也包含 xxx

如何用模式(正则表达式)在数据框中抛出行替换部分字符串