正则表达式 - 匹配任意数量的数字 [英] Regex - Matching arbitrary amount of numbers

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

问题描述

  data 
datalater
983290842
Data387428later
datafhj893724897290384later
4329804928later

我期望做的是使用正则表达式来匹配任何以数据开头并以后面的结尾且具有中间数字的行。这是我到目前为止炮制的:

  ^ [D,d] ata [0-9] * later 

然而,输出包含所有的数据删除线。我想我可以通过管道输出和grep -v datalater,但是我觉得单个表达式应该可以做到这一点。 解决方案

使用 + 而不是 *

+ 至少匹配前面的一个或多个。

* 匹配零个或多个。

  ^ [Dd] ata [0-9] + later $ 

在grep中,您需要转义 + ,我们可以使用 \

  ^ [Dd] ata\d  \ +稍后

在您的示例文件中,您也有一行:

  datafhj893724897290384later 

目前由于数据和数字之间存在字母,因此不会匹配。我们可以通过添加一个 [^ 0-9] * 来匹配数据之后的任何数字,直到数字。



我们的最终命令是:

  grep'^ [Dd] ata [^ 0-9] * \ d \ +稍后$'文件名


I've got a file that has lines in it that look similar as follows

data
datalater
983290842
Data387428later
datafhj893724897290384later
4329804928later

What I am looking to do is use regex to match any line that starts with data and ends with later AND has numbers in between. Here is what I've concocted so far:

^[D,d]ata[0-9]*later$ 

However the output includes all datalater lines. I suppose I could pipe the output and grep -v datalater, but I feel like a single expression should do the trick.

解决方案

Use + instead of *.

+ matches at least one or more of the preceding.
* matches zero or more.

^[Dd]ata[0-9]+later$

In grep you need to escape the +, and we can use \d which is a character class and matches single digits.

^[Dd]ata\d\+later$

In you example file you also have a line:

datafhj893724897290384later

This currently will not be matched due to there being letters in-between data and the numbers. We can fix this by adding a [^0-9]* to match anything after data until the digits.

Our final command will be:

grep '^[Dd]ata[^0-9]*\d\+later$' filename

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

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