Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何终止正则表达式并启动另一个正则表达式_Regex_Bash_Sed_Grep - Fatal编程技术网

Regex 如何终止正则表达式并启动另一个正则表达式

Regex 如何终止正则表达式并启动另一个正则表达式,regex,bash,sed,grep,Regex,Bash,Sed,Grep,我有一个文件,里面有这样的数据 34sdf, 434ssdf, 43fef, 34sdf, 434ssdf, 43fef, sdfsfs, 我必须识别sdfsf,并替换它和/或打印行 The exact condition is the tokens are comma separated. target expression starts with a non numeric character, and till a comma is met. 现在我用[^0-9]开始以非数字字符开头

我有一个文件,里面有这样的数据

34sdf, 434ssdf, 43fef,
34sdf, 434ssdf, 43fef, sdfsfs,
我必须识别
sdfsf,
并替换它和/或打印行

The exact condition is the tokens are comma separated. target expression starts with a non numeric character, and till a comma is met. 

现在我用
[^0-9]
开始以非数字字符开头,但下一个字符对我来说确实是未知的,它可以是数字、特殊字符、字母表,甚至是空格。所以我想要一个
(任何东西)*
。但是之前的
[]
起作用并破坏了它<代码>[^0-9]*或
[^0-9].*,
[^0-9]\+.*,
[^0-9]{1}*,
[^0-9][^,]*
[^0-9]{1}[^,]*,
到目前为止没有任何效果。因此,我的问题是如何为此编写正则表达式(从非数字字符开始,然后是除逗号以外的任何字符或逗号之前的任何字符数),我使用的是
grep
sed
(gnu)。另一个问题是对于posix或非posix,有什么区别吗?

使用sed,并对上一个正则表达式进行轻微修改:

sed -n 's/.*,[ ]*\([^ 0-9][^\,]*\),/\1/p' input

大概是这样吧

(?:(?:^(\D.*?))|(?:,\s(\D.*?))),
这将捕获以非数字字符开头的字符串。测试

我不确定sed是否支持
\D
,但如果不支持,您可以轻松地将其替换为
[^0-9]
,您已经知道了

编辑:可以修剪为:

(?:\s|^)(\D.*?),
我认为模式
(\s | ^)(\D[^,]+),
会抓住它

它匹配空格或字符串的开头以及一组非数字,后面跟逗号,后面跟逗号

如果不支持
\D
,您可以使用
[^0-9]

这可能适合您(GNU-sed):

或:


这对于示例来说越来越具体,假设sdfsfs位于行的开头,前面没有逗号或空格,那么
*,[]*
这就不能满足要求,我缺少一个检测是的,这是正确的/有效的解决方案,
sed-r's/([^0-9].*),//g'
grep-E“([^0-9].?”
两者都工作正常。感谢你的链接,它将在未来帮助我
sed '/\b[^0-9,][^,]*/!d' file # only print lines that match
sed -n 's/\b[^0-9,][^,]*/XXX/gp' file # substitute `XXX` for match