Regex 在特殊字符之间匹配数字模式
我正在尝试编写一个正则表达式,只提取数字120068018Regex 在特殊字符之间匹配数字模式,regex,Regex,我正在尝试编写一个正则表达式,只提取数字120068018 !false!|!!|!!|!!|!!|!120068018!|!!|!false! 我对正则表达式相当陌生,我发现这是一项艰巨的任务 我尝试过使用这个模式,因为数字总是以1200开头 '/^1200+!\$/' 但这似乎不起作用。这是您需要的正则表达式: /[0-9]+/ [0-9]匹配0到9之间的任何数字 +符号表示1次或多次 如果你想得到任何以1200开头的数字,那么应该是 /1200[0-9]*/ 这里我使用*,因为它允
!false!|!!|!!|!!|!!|!120068018!|!!|!false!
我对正则表达式相当陌生,我发现这是一项艰巨的任务
我尝试过使用这个模式,因为数字总是以1200开头
'/^1200+!\$/'
但这似乎不起作用。这是您需要的正则表达式:
/[0-9]+/
[0-9]匹配0到9之间的任何数字
+符号表示1次或多次
如果你想得到任何以1200开头的数字,那么应该是
/1200[0-9]*/
这里我使用*,因为它允许零或更多。否则,数字1200将无法捕获
如果要捕获(提取)字符串,请用括号将其括起来:
/(1200[0-9]*)/
使用以下命令:
/1200\d+/
\d是匹配任何数字的元字符
您的正则表达式无法工作,因为:
- 应用于前一个字符、元字符或组。所以1200+表示120后面有1个或更多的零
如果您的号码总是以
1200
开头,您可以这样做
/1200\d*/
这与以1200
加上“后面有多少位数”开头的字符串相匹配。根据您使用的正则表达式的“方言”,您可能会发现
/1200[0-9]*/
更稳健;或
/1200[[:digit:]]*/
哪个更“可读”
其中任何一个都可以用括号“捕获”。同样,根据方言的不同,您可能需要也可能不需要转义(在前面添加\
)。范例
产生
120012345
正如你所想。说明:
sed stream editor command
s substitute
^.* everything from start of string until
\( start capture group
1200 the number 1200
[0-9]* followed by any number of digits
\) end capture
.*$ everything from here to end of line
/\1/ and replace with the contents of the first capture group
什么编程语言?您至少需要一些捕获括号:
'/^(1200+!)\$/'
但是为什么要使用^
和$
锚?强烈推荐www.regex101.com作为沙盒(这解释了什么有效,为什么)。我假设您正在尝试获取字符串中的任何数字。如果你提到的数字是具体的,它应该是/120068018/但我想这没有多大意义。我要找的数字总是以1200开头,只是编辑了我的答案。
sed stream editor command
s substitute
^.* everything from start of string until
\( start capture group
1200 the number 1200
[0-9]* followed by any number of digits
\) end capture
.*$ everything from here to end of line
/\1/ and replace with the contents of the first capture group