xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Linux shell regular expression All In One

Linux shell regular expression All In One

Linux shell 正则表达式

image

keywords

The following table is a complete list of the available character class keywords in GNU sed.

下表是 GNU sed 中可用字符类关键字的完整列表。

编号 字符类 描述
1 [[:alnum:]] 字母数字 / Alphanumeric [a-z A-Z 0-9]
2 [[:alpha:]] 字母 / Alphabetic [a-z A-Z]
3 [[:blank:]] 空白字符 (空格或制表符) / Blank characters (spaces or tabs)
4 [[:cntrl:]] 控制字符 / Control characters
5 [[:digit:]] 数字 / Numbers [0-9]
6 [[:graph:]] 任何可见字符 (不包括空格) / Any visible characters (excludes whitespace)
7 [[:lower:]] 小写字母 / Lowercase letters [a-z]
8 [[:print:]] 可打印字符 (非控制字符) / Printable characters (non-control characters)
9 [[:punct:]] 标点符号 / Punctuation characters
10 [[:space:]] 空格 / Whitespace
11 [[:upper:]] 大写字母 / Uppercase letters [A-Z]
12 [[:xdigit:]] 十六进制数字 / Hex digits [0-9 a-f A-F]

https://www.tutorialspoint.com/unix/unix-regular-expressions.htm

question

regexp match IPs, which end with the 0~N spaces?

solutions

  1. $
/192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$/

image

  1. \b
/192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b/

https://regexper.com/#%2F192\.168\.(1%3F[0-9][0-9]%3F|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b%2F

  1. [[:space:]]
192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])[[:space:]]

image

https://regexper.com/

https://regex101.com/r/LY2pjB/1

demos

function test(n) {
  let reg = /192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b/;
   for (let i = 0; i < n; i++) {
     let result = reg.test(`192.168.18.${i} `);
     if(result) {
       // console.log(`192.168.18.${i} ✅`, i, result)
     } else {
       console.log(`192.168.18.${i} ❌`, i, result)
     }
   }
}

test(256);
// 192.168.18.0 ❌ 0 false
// 192.168.18.1 ❌ 1 false
// 192.168.18.255 ❌ 255 false

function test(n) {
  let reg = /192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b/;
   for (let i = 0; i < n; i++) {
     let result = reg.test(`192.168.18.${i}`);
     if(result) {
       // console.log(`192.168.18.${i} ✅`, i, result)
     } else {
       console.log(`192.168.18.${i} ❌`, i, result)
     }
   }
}

test(256);
// 192.168.18.0 ❌ 0 false
// 192.168.18.1 ❌ 1 false
// 192.168.18.255 ❌ 255 false

image

errors ❌

  1. [[:space:]] bug ❌
function test(n) {
  let reg = /192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])[[:space:]]/;
   for (let i = 0; i < n; i++) {
     let result = reg.test(`192.168.18.${i} `);
     if(result) {
       // console.log(`192.168.18.${i} ✅`, i, result)
     } else {
       console.log(`192.168.18.${i} ❌`, i, result)
     }
   }
}

test(256);

https://regexper.com/#%2F192\.168\.(1%3F[0-9][0-9]%3F|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])[[%3Aspace%3A]]%2F

  1. \s bug ❌
function test(n) {
  let reg = /192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\s?/;
   for (let i = 0; i < n; i++) {
     let result = reg.test(`192.168.18.${i}`);
     if(result) {
       // console.log(`192.168.18.${i} ✅`, i, result)
     } else {
       console.log(`192.168.18.${i} ❌`, i, result)
     }
   }
}

test(256);
// 192.168.18.0 ❌ 0 false
// 192.168.18.1 ❌ 1 false

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

MDN

JavaScript RegExp

const reg = /\w+\s/g;
// OR
const reg = new RegExp("\\w+\\s", "g");

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Special characters in regular expressions.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes

Matches a backspace. If you're looking for the word-boundary character (\b), see Assertions.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Assertions

[\b] vs \b

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Cheatsheet

⚠️ Deprecated: This feature is no longer recommended.

RegExp.$1, …, RegExp.$9

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n

refs

How to fix IP filter regular expressions written using grep command in Linux shell script All In One

如何修复在 Linux shell 脚本中使用 grep 命令编写的 IP 过滤器正则表达式?

https://www.cnblogs.com/xgqfrms/p/17356060.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-04-27 11:55  xgqfrms  阅读(22)  评论(1编辑  收藏  举报