• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jacklee404
Never Stop!
博客园    首页    新随笔    联系   管理    订阅  订阅
Regex note

Regxone note

Special Character: \ / ( ) [ ] { } + * ? | $ ^ . which are escaped characters, so you need to add \ backslash to before them.

Greedy Way

As the official python document said, It just go as far as it can.

  1. abc will match the sequence ordered.
  2. \d will match every digits [0-9]
  3. \D will match every character which is Non-digit [^0-9]
  4. . Any Charater
  5. \. Period (Some preceding charcter must using a slash eg ? ?)
  6. [abc] Only a, b, or c
  7. [^abc] Nor a, b, nor c
  8. [a-z] Characters a to z
  9. [0-9] Numbers 0 to 9
  10. \w Any Alphanumeric character [a-zA-Z_0-9]
  11. \W Any Non-alphanumeric character [^a-zA-Z_0-9]
  12. {m} m Repetitions
  13. {m, n} m to n Repetitions
  14. * Zero or more repetiotions, also denoted by {0,}
  15. + One or more repetitions, also denoted by {1,}
  16. ? Optional character, also denoted by {0, 1}
  17. \s Any Whitespace (it will match any of the specific whitespace such as space or tab or new line) [\t\n\r\f\v]
  18. \S Any Non-whitespace character [^\t\n\r\f\v]
  19. ^...$ Starts and ends
  20. (...) Capture Group
  21. (a(bc)) Capture Sub-group
  22. (.*) Capture all
  23. (abc | def) Matches abc or def

Anchors

^: Matches the beginning of a string. Example: ^(I|You) matches I or You at the start of a string.
$: Normally matches the empty string at the end of a string or just before a newline at the end of a string. Example: (\.edu|\.org|\.com)$ matches .edu, .org, or .com at the end of a string.
\b: Matches a "word boundary", the beginning or end of a word. Example: s\b matches s characters at the end of words.

Some tricks in class

  1. If you wanna non-greedy
    You should add ? after '*' or '+' to disable the greedy way that means match much as they can. (Bug it can't be useful in command sed, and you should take command perl)
posted on 2022-07-01 20:08  Jack404  阅读(8)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3