-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathregexp.rb
82 lines (65 loc) · 1.3 KB
/
regexp.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Empty
//
# Basic sequence
/abc/
# Repetition
/a*b+c?d/
/a{4,8}/
/a{,8}/
/a{3,}/
/a{7}/
# Alternation
/foo|bar/
# Character classes
/[abc]/
/[a-fA-F0-9_]/
/\A[+-]?\d+/
/[\w]+/
/\[\][123]/
/[^A-Z]/
/[]]/ # MRI gives a warning, but accepts this as matching ']'
/[^]]/ # MRI gives a warning, but accepts this as matching anything except ']'
/[^-]/
/[|]/
# Nested character classes
/[[a-f]A-F]/ # BAD - not parsed correctly
# Meta-character classes
/.*/
/.*/m
/\w+\W/
/\s\S/
/\d\D/
/\h\H/
/\n\r\t/
# Anchors
/\Gabc/
/\b!a\B/
# Groups
/(foo)*bar/
/fo(o|b)ar/
/(a|b|cd)e/
/(?::+)\w/ # Non-capturing group matching colons
# Named groups
/(?<id>\w+)/
/(?'foo'fo+)/
# Backreferences
/(a+)b+\1/
/(?<qux>q+)\s+\k<qux>+/
# Named character properties using the p-style syntax
/\p{Word}*/
/\P{Digit}+/
/\p{^Alnum}{2,3}/
/[a-f\p{Digit}]+/ # Also valid inside character classes
# Two separate character classes, each containing a single POSIX bracket expression
/[[:alpha:]][[:digit:]]/
# A single character class containing two POSIX bracket expressions
/[[:alpha:][:digit:]]/
# A single character class containing two ranges and one POSIX bracket expression
/[A-F[:digit:]a-f]/
# *Not* a POSIX bracket expression; just a regular character class.
/[:digit:]/
# Simple constant interpolation
A = "a"
/#{A}bc/
# unicode
/\u{9879}/