0

preg_match 中的正则表达式为/server\-([^\-\.\d]+)(\d+)/. 有人可以帮我理解这意味着什么吗?我看到字符串以server-但我不明白([^\-\.\d]+)(\d+)'

4

4 回答 4

2

[ ]-> 匹配 ONE 字符位置的方括号内的任何内容一次且仅一次,例如,[12] 表示将目标匹配为 1,如果不匹配则将目标匹配为 2,而 [0123456789] 表示匹配任何字符在 0 到 9 的范围内。

- -> 方括号内的 -(破折号)是“范围分隔符”,允许我们定义范围,在上面的 [0123456789] 示例中,我们可以将其重写为 [0-9]。

您可以在列表中定义多个范围,例如,[0-9A-C] 表示检查 0 到 9 和 A 到 C(但不是 a 到 c)。

注意:要测试括号内的 - (作为文字),它必须位于第一个或最后一个,也就是说,[-0-9] 将测试 - 和 0 到 9。

^-> 方括号内的 ^(抑扬符或插入符号)否定表达式(稍后我们将看到方括号外的抑扬符/插入符号的替代用法),例如,[^Ff] 表示除大写或小写 F 和 [ ^az] 表示除小写 a 到 z 之外的所有内容。

您可以在我获得此信息的来源中查看有关它的更多解释:http ://www.zytrax.com/tech/web/regex.htm

如果你想测试,你可以试试这个: http: //gskinner.com/RegExr/

于 2012-09-11T12:19:55.470 回答
1

这是解释:

# server\-([^\-\.\d]+)(\d+)
# 
# Match the characters “server” literally «server»
# Match the character “-” literally «\-»
# Match the regular expression below and capture its match into backreference number 1 «([^\-\.\d]+)»
#    Match a single character NOT present in the list below «[^\-\.\d]+»
#       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
#       A - character «\-»
#       A . character «\.»
#       A single digit 0..9 «\d»
# Match the regular expression below and capture its match into backreference number 2 «(\d+)»
#    Match a single digit 0..9 «\d+»
#       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

如果您打算使用正则表达式并愿意花费一些资金,则可以使用RegexBuddy等程序。

您也可以使用这个免费的基于网络的解释工具

于 2012-09-11T12:17:52.307 回答
1

^表示括号内不是以下字符之一

\- \.-.字符

\d是一个数字

[^\-\.\d]+表示括号内的多个字符,因此一个或多个不是 a或数字的任何字符。-.

(\d+)一个或多个数字

于 2012-09-11T12:18:30.007 回答
1

下面是perl模块给出的解释YAPE::Regex::Explain

The regular expression:

(?-imsx:server\-([^\-\.\d]+)(\d+))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  server                   'server'
----------------------------------------------------------------------
  \-                       '-'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^\-\.\d]+               any character except: '\-', '\.', digits
                             (0-9) (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2012-09-11T12:24:15.417 回答