grep 去掉 grep_使用grep的regex的10个实用示例

grep 去掉 grep

Grep or Global Regular Expression Print is used to search for text or patterns in a Linux system. It can search in files, directories, and even outputs of other commands.

Grep全局正则表达式打印用于在Linux系统中搜索文本或模式。 它可以搜索文件,目录,甚至其他命令的输出。

Regular expressions are patterns that can match text according to user’s needs. These are like rules for pattern matching.

正则表达式是可以根据用户需求匹配文本的模式。 这些就像模式匹配的规则。

Grep is often used along with regular expressions to search for patterns in text. Let’s see some practical examples of regex with grep.

Grep通常与正则表达式一起使用以搜索文本中的模式。 让我们来看一些使用grep的正则表达式的实际示例。

1.匹配单词,不论大小写 (1. Matching a word irrespective of its case)

Sometimes in a text, the same word can be written in different ways. This is most commonly the case with proper nouns. Instead of starting with an uppercase letter, sometimes they are written in all lowercase letters.

有时在文本中,可以用不同的方式写相同的单词。 最常见的情况就是专有名词。 有时不是以大写字母开头,而是全部以小写字母书写。


$ grep "[Jj]ayant" 
Grep Case

Both the versions of the word, irrespective of their case have been matched.

不论大小写,该词的两个版本均已匹配。

Another interesting case can be observed with the word ‘IoT’. A word like this might occur several times across the text with different variations. to match all the words irrespective of the case use :

可以用“ IoT”一词观察到另一个有趣的情况。 像这样的单词可能在文本中以不同的形式出现多次。 匹配所有单词,不论大小写使用:


$ grep "[iI][oO][tT]"
Grep Iot 1

2.使用带有grep的正则表达式匹配手机号码 (2. Matching mobile number using regex with grep)

Regular expressions can be used to extract mobile number from a text.

正则表达式可用于从文本中提取手机号码。

The format of the mobile number has to be known beforehand. For example, a regular expression designed to match mobile numbers won’t work for home telephone numbers.

手机号码的格式必须事先知道。 例如,旨在匹配移动电话号码的正则表达式不适用于家庭电话号码。

In this example, mobile number

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值