如何创建一个匹配正则表达式的字符串?

时间:2022-04-01 11:26:02

I need to create a formatting documentation. I know the regex that are used to format the text but I don't know how to reproduce an example for that regex. This one should be an internal link:

我需要创建一个格式化文档。我知道用于格式化文本的正则表达式,但我不知道如何重现该正则表达式的示例。这个应该是一个内部链接:

'{\[((?:\#|/)[^ ]*) ([^]]*)\]}'

Can anyone create an example that would match this, and maybe explain how he got it. I got stuck at '?'.

任何人都可以创建一个与此匹配的示例,也许可以解释他是如何得到它的。我被困在'?'。

I never used this meta-character at the beginning, usually I use it to mark that an literal cannot appear or appear exactly once.

我从来没有在开头使用过这个元字符,通常我用它来标记文字不能出现或只出现一次。

Thanks

3 个解决方案

#1


3  

(?:...) has the same grouping effect as (...), but without "capturing" the contents of the group; see http://php.net/manual/en/regexp.reference.subpatterns.php.

(?:...)具有与(...)相同的分组效果,但没有“捕获”该组的内容;请参阅http://php.net/manual/en/regexp.reference.subpatterns.php。

So, (?:\#|/) means "either # or /".

所以,(?:\#| /)表示“要么#或/”。

I'm guessing you know that [^ ]* means "zero or more characters that aren't SP", and that [^]]* means "zero or more characters that aren't right-square-brackets".

我猜你知道[^] *表示“零个或多个不是SP的字符”,而[^]] *表示“零个或多个不是右方括号的字符”。

Putting it together, one possible string is this:

把它放在一起,一个可能的字符串是这样的:

'{[/abcd asdfasefasdc]}'

#2


3  

See Open source RegexBuddy alternatives and Online regex testing for some helpful tools. It's easiest to have a regex explained by them first. I used YAPE here:

有关一些有用的工具,请参阅开源RegexBuddy替代方案和在线正则表达式测试。最先让他们解释正则表达式是最简单的。我在这里使用了YAPE:

NODE                     EXPLANATION
----------------------------------------------------------------------
  \[                       '['
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      \#                       '#'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      /                        '/'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
    [^ ]*                    any character except: ' ' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
                           ' '
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [^]]*                    any character except: ']' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  \]                       ']'
----------------------------------------------------------------------

This is under the presumption that { and } in your example are the regex delimiters.

这是假设您的示例中的{和}是正则表达式分隔符。

You can just read through the list of explanations and come up with a possible source string such as:

您只需阅读解释列表并提出可能的源字符串,例如:

 [#NOSPACE NOBRACKET]

#3


1  

I think this is a good post to help design regex. While its fairly easy to write a
general regex to match a string, sometimes its helpfull to look at it in reverse after
its designed. Sometimes it is necessary to see what bizzar things will match.

我认为这是一个很好的帖子来帮助设计正则表达式。虽然编写一般的正则表达式以匹配字符串相当容易,但有时它有助于在设计后反向查看它。有时候有必要看一下比萨的东西会匹配什么。

When mixing a lot of the metachars as literals, its fairly important to format
these kind for ease of reading and to avoid errors.

当混合大量的元文字作为文字时,格式化这些非常重要,以便于阅读和避免错误。

Here are some samples in Perl which were easier (for me) to prototype.

以下是Perl中的一些样本(对我来说)原型更容易。

my @samps = (
 '{[/abcd asdfasefasdc]}',
 '{[# ]}',
 '{[# /# \/]}',
 '{[/#  {[
    | /# {[#\/} ]}',
,
);

for (@samps) {
   if (m~{\[([#/][^ ]*) ([^]]*)\]}~)
   {
      print "Found: '$&'\ngrp1 = '$1'\ngrp2 = '$2'\n===========\n\n";
   }
}

__END__

Expanded

\{\[ 
  (
     [#/][^ ]*
  )
  [ ]
  (
     [^\]]*
  )
\]\}

Output

Found: '{[/abcd asdfasefasdc]}'
grp1 = '/abcd'
grp2 = 'asdfasefasdc'
===========

Found: '{[# ]}'
grp1 = '#'
grp2 = ''
===========

Found: '{[# /# \/]}'
grp1 = '#'
grp2 = '/# \/'
===========

Found: '{[/#    {[
        | /# {[#\/}     ]}'
grp1 = '/#      {[
        |'
grp2 = '/# {[#\/}       '
===========

#1


3  

(?:...) has the same grouping effect as (...), but without "capturing" the contents of the group; see http://php.net/manual/en/regexp.reference.subpatterns.php.

(?:...)具有与(...)相同的分组效果,但没有“捕获”该组的内容;请参阅http://php.net/manual/en/regexp.reference.subpatterns.php。

So, (?:\#|/) means "either # or /".

所以,(?:\#| /)表示“要么#或/”。

I'm guessing you know that [^ ]* means "zero or more characters that aren't SP", and that [^]]* means "zero or more characters that aren't right-square-brackets".

我猜你知道[^] *表示“零个或多个不是SP的字符”,而[^]] *表示“零个或多个不是右方括号的字符”。

Putting it together, one possible string is this:

把它放在一起,一个可能的字符串是这样的:

'{[/abcd asdfasefasdc]}'

#2


3  

See Open source RegexBuddy alternatives and Online regex testing for some helpful tools. It's easiest to have a regex explained by them first. I used YAPE here:

有关一些有用的工具,请参阅开源RegexBuddy替代方案和在线正则表达式测试。最先让他们解释正则表达式是最简单的。我在这里使用了YAPE:

NODE                     EXPLANATION
----------------------------------------------------------------------
  \[                       '['
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      \#                       '#'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      /                        '/'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
    [^ ]*                    any character except: ' ' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
                           ' '
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [^]]*                    any character except: ']' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  \]                       ']'
----------------------------------------------------------------------

This is under the presumption that { and } in your example are the regex delimiters.

这是假设您的示例中的{和}是正则表达式分隔符。

You can just read through the list of explanations and come up with a possible source string such as:

您只需阅读解释列表并提出可能的源字符串,例如:

 [#NOSPACE NOBRACKET]

#3


1  

I think this is a good post to help design regex. While its fairly easy to write a
general regex to match a string, sometimes its helpfull to look at it in reverse after
its designed. Sometimes it is necessary to see what bizzar things will match.

我认为这是一个很好的帖子来帮助设计正则表达式。虽然编写一般的正则表达式以匹配字符串相当容易,但有时它有助于在设计后反向查看它。有时候有必要看一下比萨的东西会匹配什么。

When mixing a lot of the metachars as literals, its fairly important to format
these kind for ease of reading and to avoid errors.

当混合大量的元文字作为文字时,格式化这些非常重要,以便于阅读和避免错误。

Here are some samples in Perl which were easier (for me) to prototype.

以下是Perl中的一些样本(对我来说)原型更容易。

my @samps = (
 '{[/abcd asdfasefasdc]}',
 '{[# ]}',
 '{[# /# \/]}',
 '{[/#  {[
    | /# {[#\/} ]}',
,
);

for (@samps) {
   if (m~{\[([#/][^ ]*) ([^]]*)\]}~)
   {
      print "Found: '$&'\ngrp1 = '$1'\ngrp2 = '$2'\n===========\n\n";
   }
}

__END__

Expanded

\{\[ 
  (
     [#/][^ ]*
  )
  [ ]
  (
     [^\]]*
  )
\]\}

Output

Found: '{[/abcd asdfasefasdc]}'
grp1 = '/abcd'
grp2 = 'asdfasefasdc'
===========

Found: '{[# ]}'
grp1 = '#'
grp2 = ''
===========

Found: '{[# /# \/]}'
grp1 = '#'
grp2 = '/# \/'
===========

Found: '{[/#    {[
        | /# {[#\/}     ]}'
grp1 = '/#      {[
        |'
grp2 = '/# {[#\/}       '
===========