[英]Regex: Match any character (including whitespace) except a comma
I would like to match any character and any whitespace except comma with regex. 我想将除逗号以外的任何字符和任何空白与正则表达式匹配。 Only matching any character except comma gives me: 仅匹配除逗号以外的任何字符都会给我:
[^,]*
but I also want to match any whitespace characters, tabs, space, newline, etc. anywhere in the string. 但我也想在字符串中的任何位置匹配任何空格字符,制表符,空格,换行符等。
EDIT: 编辑:
This is using sed in vim via :%s/foo/bar/gc. 通过:%s / foo / bar / gc在vim中使用sed。
I want to find starting from func up until the comma, in the following example: 在以下示例中,我想查找从func到逗号的所有内容:
func("bla bla bla"
"asdfasdfasdfasdfasdfasdf"
"asdfasdfasdf", "more strings")
I 一世
To work with multiline
in SED
using RegEx
, you should look at here . 要使用RegEx
在SED
使用multiline
,应在此处查看 。
EDIT: 编辑:
In SED
command, working with NewLine is a bit different. 在SED
命令中,使用NewLine有点不同。 SED
command support three patterns to manage multiline operations N
, P
and D
. SED
命令支持三种模式来管理多行操作N
, P
和D
To see how it works see this (Working with Multiple Lines) explaination. 要了解其工作原理,请参见此说明 (使用多行)。 Here these three operations discussed. 这里讨论这三个操作。
My guess is that N
operator is the area of consideration that is missing from here. 我的猜测是N
运算符是这里缺少的考虑领域。 Addition of N
operator will allows to sense \\n
in string. 加N
运算符将允许在字符串中感测\\n
。
An example from here : 来自这里的一个例子:
Occasionally one wishes to use a new line character in a sed script. 有时,人们希望在sed脚本中使用换行符。 Well, this has some subtle issues here. 好吧,这里有一些微妙的问题。 If one wants to search for a new line, one has to use "\\n." 如果要搜索新行,则必须使用“ \\ n”。 Here is an example where you search for a phrase, and delete the new line character after that phrase - joining two lines together. 这是一个示例,其中搜索一个短语,然后删除该短语之后的换行符-将两行连接在一起。
(echo a;echo x;echo y) | (echo a; echo x; echo y)| sed '/x$/ { N s:x\\n:x: }' sed'/ x $ / {N s:x \\ n:x:}'
which generates 产生
a xy XY
However, if you are inserting a new line, don't use "\\n" - instead insert a literal new line character: 但是,如果要插入新行,请不要使用“ \\ n”-而是插入文字换行符:
(echo a;echo x;echo y) | (echo a; echo x; echo y)| sed 's:x:X\\ :' sed's:x:X \\:'
generates 产生
a X X
y ÿ
So basically you're trying to match a pattern over multiple lines. 因此,基本上,您正在尝试在多行上匹配一个模式。
Here's one way to do it in sed
(pretty sure these are not useable within vim
though, and I don't know how to replicate this within vim
) 这是在sed
执行此操作的一种方法(虽然很确定它们在vim
中不可用,但我不知道如何在vim
复制它)
sed '
/func/{
:loop
/,/! {N; b loop}
s/[^,]*/func("ok"/
}
' inputfile
Let's say inputfile
contains these lines 比方说, inputfile
包含这些行
func("bla bla bla"
"asdfasdfasdfasdfasdfasdf"
"asdfasdfasdf", "more strings")
The output is 输出是
func("ok", "more strings")
func
, enter the braces. 如果一行包含func
,请输入大括号。 :loop
is a label named loop
:loop
是一个名为loop
的标签 ,
(that's what /,/!
means) 如果该行不包含,
(这就是/,/!
意思)
N
) 将下一行追加到模式空间( N
) loop
label ( b loop
) 分公司/去loop
标签( b loop
) ,
is found, upon which the s
command is run which matches all characters before the first comma against the (multi-line) pattern space, and performs a replacement. 因此,它将继续追加行并循环,直到找到为止,
然后运行s
命令,该命令将第一个逗号之前的所有字符与(多行)模式空间进行匹配,并执行替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.