如何让我的正则表达式提取信息,而不仅仅是检查

发布于 2024-12-12 06:59:44 字数 274 浏览 0 评论 0原文

我有一个正则表达式来检查字符串是否是邮政编码。但我真的很希望能够从完整地址(或者,如果可能的话,任何字符串)中提取它。

这是我当前的正则表达式:

/^((\d{5}-\d{4})|(\d{5})|([a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d)|([a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d))$/

如果有必要,我愿意接受一个函数(我正在使用 PHP 检查),但如果可能的话,我宁愿使用正则表达式来完成这项工作。

I have a regular expression for checking whether a string is zip/postal code or not. But I would really like to also to be able to extract that from a full address (or, if possible, any string).

Here is my current regular expression:

/^((\d{5}-\d{4})|(\d{5})|([a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d)|([a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d))$/

If necessary I'm willing to settle for a function (I'm checking with PHP) but I'd rather the regexp do the work if possible.

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

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

发布评论

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

评论(3

久而酒知 2024-12-19 06:59:44

preg_match(我假设您在根据正则表达式检查字符串时已经使用过它)也会返回与您的模式匹配的实际文本。

preg_match($regex, $input, $matches);
echo $matches[0];

第三个参数填充了尝试将正则表达式与您的输入进行匹配的结果。 $matches[0] 将包含与整个模式匹配的文本,而较高的索引将包含与捕获子模式(括号内的模式部分)匹配的文本。

但是,在您的情况下,您已使用输入开始 ^ 和输入结束 $ 字符将模式括起来,这意味着任何匹配都必须包含整个输入字符串(或多行模式下的整行)。在尝试使用此模式从较大的字符串中提取邮政编码之前,您必须删除 ^$

preg_match, which I assume you're already using when you're checking a string against your regular expression, also gives you back the actual text that matched your pattern.

preg_match($regex, $input, $matches);
echo $matches[0];

The third argument is filled with the results of trying to match the regex against your input. $matches[0] will contain text that matched the whole pattern, while higher indexes will contain text that matched against capturing subpatterns (the parts of the pattern enclosed in parentheses).

However, in your case, you've enclosed your pattern with the start-of-input ^ and end-of-input $ characters, which means that any matches must include the entire input string (or an entire line in multiline mode). You'd have to get rid of the ^ and $ before trying to use this pattern to extract a postal code from a larger string.

戏蝶舞 2024-12-19 06:59:44

PHP 会将 () 中的分组提取到一个带有 的数组中preg_match()

$matches = array();
$pattern = "/^((\d{5}-\d{4})|(\d{5})|([a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d)|([a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d))$/";
preg_match($pattern, $your_source, $matches);
print_r($matches);

PHP will extract the groupings in () into an array with preg_match():

$matches = array();
$pattern = "/^((\d{5}-\d{4})|(\d{5})|([a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d)|([a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d))$/";
preg_match($pattern, $your_source, $matches);
print_r($matches);
隔岸观火 2024-12-19 06:59:44

既然您使用的是完整地址,为什么不依赖能够准确提取和验证地址并解析其组件(包括完整邮政编码)的服务,从而提供良好的响应呢?这肯定会消除任何猜测。下面的屏幕截图显示了 SmartyStreets 的一个工具,可以从各种文本中提取地址。为了充分披露,我是 SmartyStreets 的软件开发人员。

https://smartystreets.com/account/extract

实时地址提取

Since you're working with a full address, why not rely on a service that can accurately extract and verify an address and parse it's components (including the full ZIP Code), providing a nice response? It would certainly eliminate any guessing. The screenshot below shows a tool by SmartyStreets that can extract addresses from all sorts of text. In the interest of full disclosure, I'm a software developer at SmartyStreets.

https://smartystreets.com/account/extract

LiveAddress extraction

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