这两个正则表达式可以给出不同的结果吗?

时间:2022-04-06 08:39:37

Can these two regex expressions ever give a different result?

这两个正则表达式可以给出不同的结果吗?

perl -pe 's/.*c//s'

perl -pe的/.* c // s'

perl -0777 -pe 's/.*c//s'

perl -0777 -pe的/.* c // s'

Where the .*c can be replaced with anything..

哪里。* c可以替换任何东西..

In the .*c case the result is the same

在。* c的情况下,结果是相同的

$ echo -e 'a\nb\nc\nd' | perl -pe 's/.*c//s'
a
b

d

$ echo -e 'a\nb\nc\nd' | perl -0777 -pe 's/.*c//'
a
b

d

And the question re the difference between the regexes, is where what is echoed can be replaced with anything too.

问题是正则表达式之间的差异,回声的东西也可以用任何东西代替。

Are -0777 and /s interchangeable?

-0777和/ s是否可以互换?

And is it pointless to do both -0777 with /s ?

使用/ s同时执行-0777是没有意义的吗?

2 个解决方案

#1


3  

They mean completely different things and are not interchangeable, even though in some cases they can have the same result.

它们意味着完全不同的东西,并且不可互换,即使在某些情况下它们可以具有相同的结果。

  • /s makes . match all characters (including linebreaks); without it . usually means [^\n]
  • / s使。匹配所有字符(包括换行符);没有它 。通常意味着[^ \ n]

  • -0777 means read the whole file at once; without it the file is read line by line
  • -0777表示一次读取整个文件;没有它,文件将逐行读取

/s doesn't change how the input is parsed, -0 does.

/ s不会改变解析输入的方式,-0会这样做。

-0777 is usually only useful if you are matching across several lines (in which case /s can be helpful). If you are matching line by line, then whether you use /s or not doesn't matter.

-0777通常仅在您跨多行匹配时才有用(在这种情况下/ s可能会有所帮助)。如果你逐行匹配,那么你是否使用/ s并不重要。

For example (using your example), if you would like to remove everything up to the last c, including all the lines, you could use:

例如(使用您的示例),如果您想删除最后一个c的所有内容,包括所有行,您可以使用:

echo -e 'a\nb\nc\nd' | perl -0777 -pe 's/.*c//s'

Output:

d

#2


2  

Qtax gives a good answer, i'm just going to include some examples to demonstrate they're not the same or even effectively the same.

Qtax给出了一个很好的答案,我只是要包含一些例子来证明它们不相同甚至有效。

These two

$ echo -e 'a\nb\nc\nd' | perl -pe 's/./o/s'
o
o
o
o


$ echo -e 'a\nb\nc\nd' | perl -0777 -pe 's/./o/'
o
b
c
d

These two

$ echo -e 'aa\nbb\ncc\ndd' | perl -0777 -pe 's/./o/'
oa
bb
cc
dd

$ echo -e 'aa\nbb\ncc\ndd' | perl -pe 's/./o/s'
oa
ob
oc
od

These two

$ echo -e 'aa\nbb\ncc\ndd' | perl -pe 's/./o/sg'
oooooooooooo

$ echo -e 'aa\nbb\ncc\ndd' | perl -0777 -pe 's/./o/g'
oo
oo
oo
oo

Those all demonstrate that -0777 and /s are not the same.

这些都表明-0777和/ s不一样。

#1


3  

They mean completely different things and are not interchangeable, even though in some cases they can have the same result.

它们意味着完全不同的东西,并且不可互换,即使在某些情况下它们可以具有相同的结果。

  • /s makes . match all characters (including linebreaks); without it . usually means [^\n]
  • / s使。匹配所有字符(包括换行符);没有它 。通常意味着[^ \ n]

  • -0777 means read the whole file at once; without it the file is read line by line
  • -0777表示一次读取整个文件;没有它,文件将逐行读取

/s doesn't change how the input is parsed, -0 does.

/ s不会改变解析输入的方式,-0会这样做。

-0777 is usually only useful if you are matching across several lines (in which case /s can be helpful). If you are matching line by line, then whether you use /s or not doesn't matter.

-0777通常仅在您跨多行匹配时才有用(在这种情况下/ s可能会有所帮助)。如果你逐行匹配,那么你是否使用/ s并不重要。

For example (using your example), if you would like to remove everything up to the last c, including all the lines, you could use:

例如(使用您的示例),如果您想删除最后一个c的所有内容,包括所有行,您可以使用:

echo -e 'a\nb\nc\nd' | perl -0777 -pe 's/.*c//s'

Output:

d

#2


2  

Qtax gives a good answer, i'm just going to include some examples to demonstrate they're not the same or even effectively the same.

Qtax给出了一个很好的答案,我只是要包含一些例子来证明它们不相同甚至有效。

These two

$ echo -e 'a\nb\nc\nd' | perl -pe 's/./o/s'
o
o
o
o


$ echo -e 'a\nb\nc\nd' | perl -0777 -pe 's/./o/'
o
b
c
d

These two

$ echo -e 'aa\nbb\ncc\ndd' | perl -0777 -pe 's/./o/'
oa
bb
cc
dd

$ echo -e 'aa\nbb\ncc\ndd' | perl -pe 's/./o/s'
oa
ob
oc
od

These two

$ echo -e 'aa\nbb\ncc\ndd' | perl -pe 's/./o/sg'
oooooooooooo

$ echo -e 'aa\nbb\ncc\ndd' | perl -0777 -pe 's/./o/g'
oo
oo
oo
oo

Those all demonstrate that -0777 and /s are not the same.

这些都表明-0777和/ s不一样。