简体   繁体   English

preg_match正则表达式匹配全字符串

[英]preg_match Regex Matching Full String

I have a simple regex, but it's matching more than I want... 我有一个简单的正则表达式,但是比我想要的要匹配的更多...

Basically, I'm trying to match certain operators (eg. > < != = ) followed by a string. 基本上,我试图匹配某些运算符(例如> < != = ),后跟一个字符串。

Regex: 正则表达式:

/^(<=|>=|<>|!=|=|<|>)(.*)/

Example subject: 示例主题:

>42

What I'm getting: 我得到的是:

array (size=3)
  0 => string '>42' (length=3)
  1 => string '>' (length=1)
  2 => string '42' (length=2)

What I'm trying to get: 我想要得到的是:

array (size=2)
  0 => string '>' (length=1)
  1 => string '42' (length=2)

What I don't understand is that my regex works perfectly on Regex101 我不明白的是我的正则表达式可以在Regex101上完美运行

Edit: To clarify, how can I get rid of the full string match? 编辑:为了澄清,我如何摆脱完整的字符串匹配?

Your answer is correct. 您的回答是正确的。 Group(0) is the whole match . Group(0)whole match Group(1) if first group and group(2) is the second group. Group(1)如果第一组和group(2)是第二组。

You are getting all 3 groups \\0 , \\1 , and '\\2'. 您将获得所有三个组\\0\\1和'\\ 2'。 see the group matching at the bottom of the page 在页面底部查看匹配的组

assuming your matches are in $matches you can run array_shift($matches) to remove the '\\0' match if you wish. 假设您的匹配项位于$matches ,则可以运行array_shift($matches)删除“ \\ 0”匹配项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM