Skip to content

Commit c01b839

Browse files
committed
Added RegEx Snippet
1 parent c605335 commit c01b839

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace CodeSamples.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Property)]
6+
internal sealed class DefaultValueAttribute : Attribute
7+
{
8+
private int _defaultValue;
9+
10+
public DefaultValueAttribute(int defaultValue)
11+
{
12+
_defaultValue = defaultValue;
13+
}
14+
}
15+
16+
17+
public class CustomAttributesSample
18+
{
19+
[DefaultValue(5)]
20+
public int SomeProperty { get; set; }
21+
}
22+
}

CSharp Code Samples/CodeSamples/Attributes/DebuggingSample.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ sealed class DebuggerExamplesDebuggerDisplay
6767
public string StringValue { get; set; } = "SomeStringValue";
6868
public int GetIntValue()
6969
{
70-
return 5*2;
70+
return 5 * 2;
7171
}
7272

7373
/// <summary>
@@ -106,6 +106,7 @@ public void RuntimeDebuggerAttached()
106106
{
107107
string debuggerAttached = Debugger.IsAttached ? "is" : "is not";
108108
Console.WriteLine($"Runtime Debugger Checking: Debugger {debuggerAttached} attached");
109+
Console.WriteLine($"Assuming we're running {(Debugger.IsAttached ? "from IDE" : "standalone")}");
109110
}
110111

111112
[DebuggerStepThrough]

CSharp Code Samples/CodeSamples/CodeSamples.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="Alterations\EntityConversionSample.cs" />
6464
<Compile Include="Alterations\OperatorOverloadingSample.cs" />
6565
<Compile Include="Attributes\ConditionalSample.cs" />
66+
<Compile Include="Attributes\CustomAttributesSample.cs" />
6667
<Compile Include="Attributes\ObsoleteSample.cs" />
6768
<Compile Include="Classes\AccessModifiersSample.cs" />
6869
<Compile Include="Classes\AnonymousTypesSample.cs" />
@@ -104,6 +105,7 @@
104105
<DesignTimeSharedInput>True</DesignTimeSharedInput>
105106
<DependentUpon>Settings.settings</DependentUpon>
106107
</Compile>
108+
<Compile Include="RegularExpressions\RegExSample.cs" />
107109
<Compile Include="SampleExecute.cs" />
108110
<Compile Include="Settings.cs" />
109111
<Compile Include="Settings\ConfigurationManagerSnippets.cs" />

CSharp Code Samples/CodeSamples/Program.cs

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using CodeSamples.Files;
99
using CodeSamples.MultiThreading;
1010
using CodeSamples.Patterns;
11+
using CodeSamples.RegularExpressions;
1112
using CodeSamples.Settings;
1213
using CodeSamples.SOLID.S01_SingleResponsibilityPrinciple_SRP;
1314
using CodeSamples.SOLID.S04_InversionOfControl_IoC;
@@ -193,6 +194,11 @@ static void Main(string[] args)
193194
collectionInitializerSamples.Execute();
194195
#endregion
195196

197+
#region [Regular Expressions]
198+
var regularExpressionSample = new RegExSample();
199+
regularExpressionSample.Execute();
200+
#endregion
201+
196202
Console.WriteLine();
197203
Console.WriteLine("End Code Samples");
198204

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Text.RegularExpressions;
3+
4+
namespace CodeSamples.RegularExpressions
5+
{
6+
public class RegExSample : SampleExecute
7+
{
8+
private void IsMatchSnipet()
9+
{
10+
string pattern = @"^This sentence";
11+
string sentenceYes = $"This sentence is a match.";
12+
string sentenceNo = $"This weird sentence is not a match.";
13+
14+
Console.WriteLine($"The sentence '{sentenceYes}' is {(Regex.IsMatch(sentenceYes, pattern) ? "" : "not")} a match to pattern '{pattern}'");
15+
Console.WriteLine($"The sentence '{sentenceNo}' is {(Regex.IsMatch(sentenceNo, pattern) ? "" : "not")} a match to pattern '{pattern}'");
16+
}
17+
18+
public override void Execute()
19+
{
20+
Title("RegExSampleExecute");
21+
22+
IsMatchSnipet();
23+
24+
Finish();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)