-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMatch_n.html.html.html
More file actions
91 lines (89 loc) · 3.45 KB
/
Match_n.html.html.html
File metadata and controls
91 lines (89 loc) · 3.45 KB
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
83
84
85
86
87
88
89
90
91
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>Match n</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.min.js"
integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc"
crossorigin="anonymous"
></script>
</head>
<body>
<h1 id="match-n">Match n</h1>
<p>String length</p>
<p><code>^.{1,35}$</code></p>
<p>String that containts a substring ONLY once</p>
<p><code>^(?:(?!<scr>).)*<scr>(?!.*<scr>).*$</code></p>
<p>No identical consecutive characters</p>
<p><code>^(([\w])(?!\2))+$</code></p>
<p>No more than two identical consecutive characters, etc</p>
<p><code>^(([\w])\2?(?!\2))+$</code></p>
<h3
id="at-least-two-unique-characters-httpsstackoverflow.comquestions5341369regex-for-at-least-two-unique-characters"
>
At least two unique characters:
https://stackoverflow.com/questions/5341369/regex-for-at-least-two-unique-characters/
</h3>
<pre><code>^
(?> # Possessive group - do not backtrack!
(.) # Match a duplicated character
(?:
(?=.*\1) # It can have a duplicate after itself
|
(?<=\1.+) # Or it already had one
)
| # Or, it isn't a duplicated character at all
(?<Unique>.) # Capture it as a unique character.
)+
$
(?<-Unique>){2} # After we're done, check there were at least
# two unique characters</code></pre>
<p>
The idea here is to match duplicated characters and count the rest using
balancing groups: This is achieved with a possessive group - it makes sure
the duplicated characters never backtrack, thus the next dot will only
capture a non-duplicated character.<br />
In .Net, every capture of a group is added to a stack. (?<-Unique>)
pops a capture from the stack, and fails if it’s empty. It gives a nice
way of counting how many captures we’ve had.
</p>
<p>Or a simpler solution:</p>
<pre><code>(.)(?<!\1.+)(?!.*\1).*(.)(?<!\2.+)(?!.*\2)</code></pre>
<p>To explain it:</p>
<pre><code>(.) # match any character...
(?<!\1.+) # ...which does not already exist in the input...
(?!.*\1) # ..and does not exist later on in the input
# We have now found one unique character.
.* # allow for any number of random characters in the middle
(.)(?<!\2.+)(?!.*\2) # Find a second unique character,
# using the same technique.</code></pre>
</body>
</html>