-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLinQChains.html.html.html
68 lines (67 loc) · 2.39 KB
/
LinQChains.html.html.html
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
<!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>LinQChains</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/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc"
crossorigin="anonymous"
></script>
</head>
<body>
<h1 id="some-fun-with-regex-and-linq">Some Fun with Regex and LinQ</h1>
<h2 id="projectionmapping">Projection/Mapping</h2>
<p>Project match result directly to formated result string</p>
<pre><code>RegEx.Matches(myText,searchPatten).Cast<Match>().Select(x => $"{x.Groups[1].Value} {x.Groups[2].Value}").ToArray();</code></pre>
<h2 id="extend-result-with-select-new">Extend result with Select new</h2>
<p>
Here : Match a pattern and apply Replace on the resulting Match
Collection, something like.
<code>RegEx.MatchAndReplace(myText,searchPattern,replacePattern);</code>
</p>
<p>
This allows us to get the Index and the Length of each match along what it
would be replaced with.
</p>
<!-- We want to use LinQ since Regex.Replace internally uses the Matches but doesn't expose its content, nor Regex expose the interpreter it uses for the replacement pattern. -->
<pre><code>use Regex.Matches() and then on every Match do a Regex.Replace()
var regex = new Regex(searchPattern);
var matches = regex.Matches(myText)
.Cast<Match>()
.Select(x => new
{
Match = x,
ReplacedWith = regex.Replace(x.Value, replacePatten)
})
.ToArray();
```</code></pre>
</body>
</html>