-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLinQChains.md.html
More file actions
56 lines (55 loc) · 1.74 KB
/
Copy pathLinQChains.md.html
File metadata and controls
56 lines (55 loc) · 1.74 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
<!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.md</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>
</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>
<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>