正则表达式匹配具有电话号码的文本中正好6个数字的字符串

时间:2022-01-01 22:18:57

I have blocks of free text that contain phone numbers and multiple 6 digit numbers that I need to capture. The 6 digit number has an optional ','.

我有*文本块,包含我需要捕获的电话号码和多个6位数字。 6位数字有一个可选的','。

examples of the 6 digit numbers could be 123456, or 123,456, but I need to differentiate from a phone number like 1 234 456 8901

6位数字的例子可能是123456,或123,456,但我需要区分电话号码,例如1 234 456 8901

I have :

我有 :

    preg_match_all(",\[\W_][0-9]{3}(?:,)[0-9]{3}[\W_][\D]\d",$html, $value); 

Is there a better way to do this?

有一个更好的方法吗?

1 个解决方案

#1


2  

It's a bit difficult to review the regex without the sample input but couple of observations:

在没有样本输入的情况下查看正则表达式有点困难,但有几点观察:

  • [0-9] can be replaced with \d (since, you're already using it at the end)

    [0-9]可以替换为\ d(因为,你最后已经在使用它了)

  • [\D] is exactly the same as \D. It's a character class itself and unless you have some more characters to include it'ss fine without being enclosed in [].

    [\ D]与\ D完全相同。它本身就是一个角色类,除非你有更多的角色要包含它,否则不会被包含在[]中。

  • (?:,) should simply be , because you neither want to capture it nor it has any quantifiers.

    (?:,)应该只是,因为你既不想捕获它也不想有任何量词。

  • ,\[\W_] Here it seems you want to use the character class but the \ would escape the first [. If you actually need a literal \ there; you need to escape it as \\ since it's a special character.

    ,\ [\ W_]这里似乎你想要使用字符类但是\会逃脱第一个[。如果你真的需要文字\那里;你需要把它作为\\来逃避它,因为它是一个特殊的角色。

#1


2  

It's a bit difficult to review the regex without the sample input but couple of observations:

在没有样本输入的情况下查看正则表达式有点困难,但有几点观察:

  • [0-9] can be replaced with \d (since, you're already using it at the end)

    [0-9]可以替换为\ d(因为,你最后已经在使用它了)

  • [\D] is exactly the same as \D. It's a character class itself and unless you have some more characters to include it'ss fine without being enclosed in [].

    [\ D]与\ D完全相同。它本身就是一个角色类,除非你有更多的角色要包含它,否则不会被包含在[]中。

  • (?:,) should simply be , because you neither want to capture it nor it has any quantifiers.

    (?:,)应该只是,因为你既不想捕获它也不想有任何量词。

  • ,\[\W_] Here it seems you want to use the character class but the \ would escape the first [. If you actually need a literal \ there; you need to escape it as \\ since it's a special character.

    ,\ [\ W_]这里似乎你想要使用字符类但是\会逃脱第一个[。如果你真的需要文字\那里;你需要把它作为\\来逃避它,因为它是一个特殊的角色。