Skip to content

Latest commit

 

History

History
155 lines (109 loc) · 2.84 KB

regex.md

File metadata and controls

155 lines (109 loc) · 2.84 KB

Regular Expression

a sequence of characters that specifies a search pattern


Reference

Online tester & debugger: PHP, PCRE, Python, Golang & JavaScript

Intro

Since the 1980s, different syntaxes for writing regular expressions exist, one being the POSIX standard and another, widely used, being the Perl syntax.

Usage: todo oneday

CH & EN

Add whitespaces between Chinese & English words ( imperfect )

([^a-zA-Z0-9`'"_\-\s\(\),\.#\[\]=?{}/*@:])([a-zA-Z0-9`'"_\-\(\),\.#\[\]=?{}/*@:]+)([^a-zA-Z0-9`'"_\-\s\(\),\.#\[\]=?{}/*@:])
$1 $2 $3

Comma

Add a whitespace after comma ,

,([^ \n]+)
, $1

Date

Date Range

([0-9]{4})\s*?[/\-.年]\s*?([0-9]{1,2})\s*?[/\-.月]\s*?([0-9]{1,2})[日]?
# 2020年01月12日 ~ 2020 年 1 月 13 日

HTML Tags

Replace HTML Tag

  • Bold
<b[^>]*>([^<]*)</b>
\*\*$1\*\*
  • Image
<img[^>]*src="([^"]*)"[^>]*/>
![]($1)
  • Link
<a href="([^"]*)"[^>]*>([^<]*)</a>
[$2]($1)

MD Link

Link Match

\[([^\]]+)\]\(([^\)]+)\)

# Title : $1
# Link : $2

Params

Find function with 7 params

functionName\(([^,^;]*,\s?){6}([^;^,]*?)\)

Quote

Replace "" with ''

"([^"]*)"
'$1'

Square Brackets

^\[[^\]]*\]
# e.g.
[ERROR]
[WARN]
[INFO]

Exact 2 Spaces Not at the Head of Line

# original
(?<!^)  (?! )

# improve 1
(?<!^)\s{2}(?! )

# improve 2
(?<!^| )\s{2}(?! )

# improve 3
(?<!^|\s)\s{2}(?!\s)

Pattern Match

  • (pattern) 匹配获取 Capturing Parenthesis
  • (?:pattern) 非匹配获取 Non-Capturing Parenthesis
    • e.g. (Windows )(?:\d+)
      • Windows 98 matched
      • Windows 2000 matched
      • Windows XP not matched
  • (?=pattern) 正向肯定预查 Lookahead Positive Assertions
    • e.g. Windows (?=98)
      • Windows 98 matched
      • Windows XP not matched
  • (?!pattern) 正向否定预查 Lookahead Negative Assertions
    • e.g. Windows (?!98)
      • Windows XP matched
      • Windows 98 not matched
  • (?<=pattern) 反向肯定预查 Lookbehind Positive Assertions
    • e.g. (?<=My) Windows
      • My Windows matched
      • X Windows not matched
  • (?<!pattern) 反向否定预查 Lookbehind Negative Assertions
    • e.g. (?<!My) Windows
      • X Windows matched
      • My Windows not matched