用于从电话号码中提取业务分机号的正则表达式

发布于 2024-11-15 21:59:13 字数 283 浏览 4 评论 0原文

我正在尝试编写一个正则表达式来从完整的电话号码字符串中提取电话分机号。这应该适用于这样的数字:777.777.7777 x 7302

它也应该适用于使用“ext”、“EXT”、“Ext”、“eXt”、“ext.”和“Ext”。本质上只是涵盖了它的所有常见用途。

我只需要“x 7302”部分。事实上,一旦我提取它,我将把它只剩下分机号。

有人可以帮我吗?当正则表达式变得更加复杂时,我会遇到一些困难。

我正在 PHP 函数(preg_match)中执行此操作,如果这对任何人都有帮助的话。

I am having the dang-est time trying to write a regex that will extract the phone extension from a full phone number string. This should work on a number like this one: 777.777.7777 x 7302

It should also work using "ext", "EXT", "Ext", "eXt", "ext.", and "Ext ". Essentially just cover all the common ground use of it.

I just need the "x 7302" part. In fact I am just going to strip it down to just the extension number once I extract it.

Can anyone help me please? Regular expressions are something that I struggle with when they get more complex.

I am doing this in a PHP function (preg_match) if that will help anyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

和影子一齐双人舞 2024-11-22 21:59:13

这可能有助于为您提供一些帮助:

$phonenumber = '777.777.7777 x 7302';

$extension = preg_replace('(^.*(ext|x) ?([0-9]+)$)i', '$2', $phonenumber);

echo $extension;

使用 i 修饰符(在末尾)使正则表达式不区分大小写,以便匹配 ext 的所有组合。我使用一个组来提供这两种变体:extx(ext|x)

剩下的就是寻找末尾的数字,EXT 和数字之间可以有空格。

This probably helps to give you something to play with:

$phonenumber = '777.777.7777 x 7302';

$extension = preg_replace('(^.*(ext|x) ?([0-9]+)$)i', '$2', $phonenumber);

echo $extension;

Use the i modifier (at the end) to make the regex case insensitive so to match all combinations of ext. I used a group to offer both variant: ext or x: (ext|x).

The rest is looking for a number at the end, and a space is possible between EXT and the number.

雾里花 2024-11-22 21:59:13

尝试使用正则表达式:

/e?xt?\.?\s*\d+$/i 

<?

echo "<pre>";
     $phone = '777.777.7777 x 7302'; 
        preg_match("/e?xt?\.?\s*\d+$/i", $phone, $matched); 
        print_r($matched);
?>

输出:

Array
(
    [0] => x 7302
)

try with regex:

/e?xt?\.?\s*\d+$/i 

<?

echo "<pre>";
     $phone = '777.777.7777 x 7302'; 
        preg_match("/e?xt?\.?\s*\d+$/i", $phone, $matched); 
        print_r($matched);
?>

Output:

Array
(
    [0] => x 7302
)
世界如花海般美丽 2024-11-22 21:59:13
\w+\s*\d+$

这是一个更简单的正则表达式,假设输入与您提供的内容类似。

\w+\s*\d+$

It is an simpler regex assuming that the input is similar to what you have provided.

南风几经秋 2024-11-22 21:59:13

这应该为您做

/([\d\-\.]+)([ext\. ]+)(\d+)/i

第一组匹配由破折号或点分隔的数字。第二组与您的分机字符串匹配,第三组与您的分机号码匹配。

This should do it for you

/([\d\-\.]+)([ext\. ]+)(\d+)/i

The first set matches the numbers separated by dash or dot. The second set matches your extension string and the third set matches your extension number.

你是暖光i 2024-11-22 21:59:13

如果您只需要字符串的最后一个数字,则可以使用:

\D+(\d+)$

\D+ 至少一位非数字,后跟:

(\d+) 至少一位数字(捕获使用括号)

$ 在字符串末尾。

If you just want the last numbers of the string, you can use:

\D+(\d+)$

\D+ at least one non-digit followed by:

(\d+) at least one digit (captured using parenthesis)

$ at the end of the string.

著墨染雨君画夕 2024-11-22 21:59:13

我对正则表达式有点怀疑,所以当我说是否可以使用 PHP 函数“explode()”分割字符串并使用“x”字符作为分隔符时,我有点偏见?

如果您不熟悉该函数,这里是该函数的 PHP 手册的链接:

Explode( )

I am a bit leery of regular expression, so I'm a bit biased when I say, is it possible to just split the string using the PHP function "explode()", using the 'x' character as your delimiter?

Here is a link to the PHP manual for that function if you are not familiar with it:

Explode()

澜川若宁 2024-11-22 21:59:13

试试这个功能:

$pn = "777.777.7777 x 7302"; 

function get_ext($pn)
{
$ext = preg_replace('/[\d\.\s]+[ext\s]+(\d{1,})/i', '$1', $pn);
return $ext;
}

echo get_ext($pn);
//7302

Try this function:

$pn = "777.777.7777 x 7302"; 

function get_ext($pn)
{
$ext = preg_replace('/[\d\.\s]+[ext\s]+(\d{1,})/i', '$1', $pn);
return $ext;
}

echo get_ext($pn);
//7302
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文