正则表达式“/ \\ * {2,} /”是什么意思? [重复]

时间:2023-01-18 16:55:59

This question already has an answer here:

这个问题在这里已有答案:

I'm kinda new to regex, and specifically, I don't understand there are 2 backslashes? I mean, I know the second one is to escape the character "*", but what does the first backslash do?

我对正则表达式有点新意,具体来说,我不明白有2个反斜杠?我的意思是,我知道第二个是逃避字符“*”,但第一个反斜杠的作用是什么?

Well I'm passing this regex expression to the php function preg_match(), and I'm trying to find strings that include 2 or more consecutive "*".

好吧,我将这个正则表达式表达式传递给php函数preg_match(),我正在尝试查找包含2个或更多连续“*”的字符串。

4 个解决方案

#1


That regex is invalid syntax.

那个正则表达式是无效的语法。

You have this piece:

你有这件作品:

*{2,}

Which basically would read: match n-times, 2 or more times.

基本上会读到:匹配n次,2次或更多次。


The following regex:

以下正则表达式:

/\\*.{2,}/

Is the simplest and closest regex to the one you have, which would read as:
match 0 or more '\' and 2 or more characters that aren't newlines

是最简单和最接近的正则表达式,它可以读作:匹配0或更多'\'和2个或更多不是换行符的字符


If you are talking about the string itself, is may be interpreted as 2 things:

如果你在谈论字符串本身,可能会被解释为两件事:

  • /\\*{2,}/
    Read as: match a single \ and another \ n-times 2 times or more
    This is invalid syntax
  • / \\ * {2,} /读为:匹配单个\和另一个\ n次2次或更多这是无效的语法

  • /\*{2,}\
    Read as match 2 or more * This is valid syntax
  • / \ * {2,} \读取匹配2或更多*这是有效的语法

It all varies, depending on the escape character.

这一切都有所不同,具体取决于逃脱特征。


Edit:

Since the question was updated to show which language and engine it is being used, I've updated to add the following information:

由于问题已更新以显示正在使用的语言和引擎,因此我已更新以添加以下信息:

You have to pass the regex as '/\*{2,}/' OR as "/\\*{2,}/" (watch the quotes).

你必须将正则表达式作为'/ \ * {2,} /'或“或”作为“/ \\ * {2,} /”(注意引号)。

Both are very similar, except that single quotes ('') only support the following escape sequences:

两者都非常相似,只是单引号('')仅支持以下转义序列:

  • \' - Produces '
  • \' - 生产'

  • \\- Produces \
  • \\ - 制作\

Double-quoted strings are treated differently in PHP. And they support almost any escape sequence, like:

在PHP中,双引号字符串的处理方式不同。它们几乎支持任何转义序列,例如:

  • \" - Produces "
  • \“ - 生产”

  • \' - Produces '
  • \' - 生产'

  • \\ - Produces \
  • \\ - 制作\

  • \x<2-digit hex number> - Same as chr(0x<2-digit hex number>)
  • \ x <2位十六进制数> - 与chr相同(0x <2位十六进制数>)

  • \0 - Produces a null char
  • \ 0 - 生成空字符

  • \1 - Produces a control char (same as chr(1))
  • \ 1 - 生成一个控制字符(与chr(1)相同)

  • \u<4-digit hex number> - Produces an UTF-8 character
  • \ u <4位十六进制数> - 生成UTF-8字符

  • \r - Produces a newline on old OSX
  • \ r \ n - 在旧OSX上生成换行符

  • \n - Produces a newline on Linux/newer OSX/Windows (when writting a file without b)
  • \ n - 在Linux /更新的OSX / Windows上生成换行符(在没有b的情况下写入文件时)

  • \t - Produces a tab
  • \ t - 生成选项卡

  • \<number> or \0<number> - Same as \x, but the numbers are in octal (e.g.: "\75" and "\075" produce =)
  • \ 或\ 0 - 与\ x相同,但数字是八进制的(例如:“\ 75”和“\ 075”产生=)

  • ... (some more that I probably forgot) ...
  • ......(我可能忘了更多)......

  • \<anything> - Produces <anything>
  • \ - 生成

Read more about this on https://php.net/manual/en/language.types.string.php

有关详细信息,请访问https://php.net/manual/en/language.types.string.php

#2


Depending on the platrofm you're using, "/\\*{2,}/" may actually be a representation of a /\*{2,'}/ string - this is because languages like Java treat \ as an escape character, so to actually put that character within regex, you need to escape the character in regex string.

根据您正在使用的platrofm,“/ \\ * {2,} /”实际上可能是/ \ * {2,'} / string的表示 - 这是因为像Java这样的语言将\视为转义字符,所以要真正把这个字符放在正则表达式中,你需要转义正则表达式字符串中的字符。

So, we have /\*{2'}/ regex. \*' matches the star character, and{2,}` means at least two times. Your regex will match any two or more consecutive star characters.

所以,我们有/ \ * {2'} /正则表达式。 \ *'匹配星号,{2,}`表示至少两次。您的正则表达式将匹配任何两个或更多连续的明星字符。

#3


Is it a string literal written in a program and if so which one? The double backslash may be to escape the escape char so that this regex matches at least 2 * star characters.

它是一个在程序中编写的字符串文字,如果是这样的话?双反斜杠可能是为了逃避转义字符,以便此正则表达式匹配至少2 *个星号字符。

In JavaScript for example you need to escape the \ so that your string literal can express it as data before you transform it into a regular expression when using the RegExp constructor. Why do regex constructors need to be double escaped?

例如,在JavaScript中,您需要转义\,以便在使用RegExp构造函数将字符串文字转换为正则表达式之前,您的字符串文字可以将其表示为数据。为什么正则表达式构造函数需要双重转义?

#4


For PHP what you have with that regex is to repeat literally a * 2 or more times. You can easily see with with below diagram:

对于PHP,你使用的正则表达式是字面上重复* 2次或更多次。您可以使用下图轻松查看:

正则表达式“/ \\ * {2,} /”是什么意思? [重复]

But when you have to code it in PHP you have to escape the backslash (with a backslash) to use it in string. For instance:

但是当你必须在PHP中编写代码时,你必须转义反斜杠(使用反斜杠)才能在字符串中使用它。例如:

$re = "/\\*{2,}/"; 
$str = "..."; 

preg_match($re, $str, $matches);

#1


That regex is invalid syntax.

那个正则表达式是无效的语法。

You have this piece:

你有这件作品:

*{2,}

Which basically would read: match n-times, 2 or more times.

基本上会读到:匹配n次,2次或更多次。


The following regex:

以下正则表达式:

/\\*.{2,}/

Is the simplest and closest regex to the one you have, which would read as:
match 0 or more '\' and 2 or more characters that aren't newlines

是最简单和最接近的正则表达式,它可以读作:匹配0或更多'\'和2个或更多不是换行符的字符


If you are talking about the string itself, is may be interpreted as 2 things:

如果你在谈论字符串本身,可能会被解释为两件事:

  • /\\*{2,}/
    Read as: match a single \ and another \ n-times 2 times or more
    This is invalid syntax
  • / \\ * {2,} /读为:匹配单个\和另一个\ n次2次或更多这是无效的语法

  • /\*{2,}\
    Read as match 2 or more * This is valid syntax
  • / \ * {2,} \读取匹配2或更多*这是有效的语法

It all varies, depending on the escape character.

这一切都有所不同,具体取决于逃脱特征。


Edit:

Since the question was updated to show which language and engine it is being used, I've updated to add the following information:

由于问题已更新以显示正在使用的语言和引擎,因此我已更新以添加以下信息:

You have to pass the regex as '/\*{2,}/' OR as "/\\*{2,}/" (watch the quotes).

你必须将正则表达式作为'/ \ * {2,} /'或“或”作为“/ \\ * {2,} /”(注意引号)。

Both are very similar, except that single quotes ('') only support the following escape sequences:

两者都非常相似,只是单引号('')仅支持以下转义序列:

  • \' - Produces '
  • \' - 生产'

  • \\- Produces \
  • \\ - 制作\

Double-quoted strings are treated differently in PHP. And they support almost any escape sequence, like:

在PHP中,双引号字符串的处理方式不同。它们几乎支持任何转义序列,例如:

  • \" - Produces "
  • \“ - 生产”

  • \' - Produces '
  • \' - 生产'

  • \\ - Produces \
  • \\ - 制作\

  • \x<2-digit hex number> - Same as chr(0x<2-digit hex number>)
  • \ x <2位十六进制数> - 与chr相同(0x <2位十六进制数>)

  • \0 - Produces a null char
  • \ 0 - 生成空字符

  • \1 - Produces a control char (same as chr(1))
  • \ 1 - 生成一个控制字符(与chr(1)相同)

  • \u<4-digit hex number> - Produces an UTF-8 character
  • \ u <4位十六进制数> - 生成UTF-8字符

  • \r - Produces a newline on old OSX
  • \ r \ n - 在旧OSX上生成换行符

  • \n - Produces a newline on Linux/newer OSX/Windows (when writting a file without b)
  • \ n - 在Linux /更新的OSX / Windows上生成换行符(在没有b的情况下写入文件时)

  • \t - Produces a tab
  • \ t - 生成选项卡

  • \<number> or \0<number> - Same as \x, but the numbers are in octal (e.g.: "\75" and "\075" produce =)
  • \ 或\ 0 - 与\ x相同,但数字是八进制的(例如:“\ 75”和“\ 075”产生=)

  • ... (some more that I probably forgot) ...
  • ......(我可能忘了更多)......

  • \<anything> - Produces <anything>
  • \ - 生成

Read more about this on https://php.net/manual/en/language.types.string.php

有关详细信息,请访问https://php.net/manual/en/language.types.string.php

#2


Depending on the platrofm you're using, "/\\*{2,}/" may actually be a representation of a /\*{2,'}/ string - this is because languages like Java treat \ as an escape character, so to actually put that character within regex, you need to escape the character in regex string.

根据您正在使用的platrofm,“/ \\ * {2,} /”实际上可能是/ \ * {2,'} / string的表示 - 这是因为像Java这样的语言将\视为转义字符,所以要真正把这个字符放在正则表达式中,你需要转义正则表达式字符串中的字符。

So, we have /\*{2'}/ regex. \*' matches the star character, and{2,}` means at least two times. Your regex will match any two or more consecutive star characters.

所以,我们有/ \ * {2'} /正则表达式。 \ *'匹配星号,{2,}`表示至少两次。您的正则表达式将匹配任何两个或更多连续的明星字符。

#3


Is it a string literal written in a program and if so which one? The double backslash may be to escape the escape char so that this regex matches at least 2 * star characters.

它是一个在程序中编写的字符串文字,如果是这样的话?双反斜杠可能是为了逃避转义字符,以便此正则表达式匹配至少2 *个星号字符。

In JavaScript for example you need to escape the \ so that your string literal can express it as data before you transform it into a regular expression when using the RegExp constructor. Why do regex constructors need to be double escaped?

例如,在JavaScript中,您需要转义\,以便在使用RegExp构造函数将字符串文字转换为正则表达式之前,您的字符串文字可以将其表示为数据。为什么正则表达式构造函数需要双重转义?

#4


For PHP what you have with that regex is to repeat literally a * 2 or more times. You can easily see with with below diagram:

对于PHP,你使用的正则表达式是字面上重复* 2次或更多次。您可以使用下图轻松查看:

正则表达式“/ \\ * {2,} /”是什么意思? [重复]

But when you have to code it in PHP you have to escape the backslash (with a backslash) to use it in string. For instance:

但是当你必须在PHP中编写代码时,你必须转义反斜杠(使用反斜杠)才能在字符串中使用它。例如:

$re = "/\\*{2,}/"; 
$str = "..."; 

preg_match($re, $str, $matches);