字符串集的正则表达式

Regular expression for set of strings

我要生成三个正则表达式

  1. 首先是包含2G的字符串
  2. 其次是包含3G的字符串
  3. 第三个是包含4G的字符串

这组字符串是:

- 4 GB+ 2 GB Night 3G/2G Data    #matches the exp generated by 1 and 2
- 500 MB 4G/3G data              #matches the exp generated by 3 and four 
- 3GB 2G/3G/4G data              #matches all the three
- 2GB 4G/3G/2G Data

我的表达也是捕获'2GB''3GB''4GB'。我想摆脱包含 'B' 的 exp。我是普通人的新手 expression.Please 建议上面的正确表达式。

要区分 3GB3G,您需要确保术语在 G 之后结束。这可以使用 字边界 来完成。在正则表达式中,它是用序列 \b.

完成的

因此 \b[2-4]G\b 确保在 234 之前没有字母或数字,在 G.

Here's an illustration what it matches, and some that it doesn't, at regex101.