正则表达式匹配包含数字的单词 [英] Regex matching a word with numbers in it

查看:121
本文介绍了正则表达式匹配包含数字的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Text::Ngrams 来确定字符串中的单词组合.但是,我需要保留其中包含数字的单词.我已经确定 $o->{tokenrex} 是我需要修改的,但我无法确定合适的正则表达式.

I'm using Text::Ngrams to determine the word combinations in a string. However, I need to keep words that have digits in them. I've determined that $o->{tokenrex} is what I need to modify, but I can't determine the proper regex for it.

原文是qr/([a-zA-Z]+|(\d+(\.\d+)?|\d*\.\d+)([eE][-+]?\d+)?)/; 但我想我需要更多类似的东西:

The original is qr/([a-zA-Z]+|(\d+(\.\d+)?|\d*\.\d+)([eE][-+]?\d+)?)/; but I'm thinking I need something more along the lines of this:

 qr/([a-zA-Z]+|(?<=\w)(\d+(\.\d+)?|\d*\.\d+)([eE][-+]?\d+)?(?=\w)|(\d+(\.\d+)?|\d*\.\d+)([eE][-+]?\d+)?)/;

如果我正确阅读正则表达式,哪个应该匹配任意数量的字母字符,或者在它之前和之后有一个单词字符的数字",或者一个数字".除了它将我的单词"拆分为单独的标记.我使用的示例词是A1X".

Which should, if I'm reading regex right, match any number of alpha characters, or a "number" that has a word character before and after it, or a "number". Except that it's splitting up my "word" into separate tokens. The example word I'm working with is "A1X".

任何帮助都会很棒.

推荐答案

你们都把这种方式变得太复杂了.原始正则表达式匹配仅由字母或数字(整数、浮点数,包括指数表示法)组成的单词.

Y'all are making this way too complicated. The original regex matches words made of letters only or numbers (integers, floating point including exponential notation).

如果您需要匹配由字母和数字组成的单词,则其正则表达式为 [a-zA-Z\d]+.根据模块文档,您还需要指定要跳过的内容,并且匹配 [^a-zA-Z\d]+.

If you need to match words made of letters and numbers, then the regex for that is [a-zA-Z\d]+. Per the module docs, you'll also want to specify what to skip, and that matches [^a-zA-Z\d]+.

$self->{tokenrex} = qr/([a-z\d]+)/i;
$self->{skiprex}  = qr/([^a-z\d]+)/i;

如果您需要识别数字,如模块文档在其示例中所示,请告诉我,我很乐意为您重新添加.根据您的描述,这听起来不像您所需要的.

If you need to recognize numbers as the module documentation shows in its example, then please let me know, and I'll be happy to add that back in for you. From your description, that doesn't sound like what you need.

这篇关于正则表达式匹配包含数字的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆