Skip to content

Commit

Permalink
Reorganized examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstrempel committed Oct 25, 2024
1 parent ec2101d commit a241aa8
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 36 deletions.
38 changes: 38 additions & 0 deletions examples/Anexia.Gregex.Examples/AnyExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ------------------------------------------------------------------------------------------
// <copyright file = "AnyExample.cs" company = "ANEXIA® Internetdienstleistungs GmbH">
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------------------


namespace Anexia.Gregex.Examples;

/// <summary>
/// Example for how to use the <see cref="Gregex.Any{T}"/> method for matching any element.
/// </summary>
public static class AnyExample
{
public static void main()
{
var listOfWords = new List<string>()
{
"Hello",
"World",
"This",
"Is",
"A",
"Test",
"For",
"Any",
"Regex"
};
var anyString = Gregex.Any<string>();

var matcher = new Matcher<string>();

var matches = matcher.FindMatches(anyString, listOfWords);

Console.WriteLine(string.Join(Environment.NewLine, matches));

}
}
29 changes: 29 additions & 0 deletions examples/Anexia.Gregex.Examples/AtLeastOnceExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ------------------------------------------------------------------------------------------
// <copyright file = "AtLeastOnceExample.cs" company = "ANEXIA® Internetdienstleistungs GmbH">
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------------------

namespace Anexia.Gregex.Examples;

/// <summary>
/// Example of how to use the <see cref="Gregex.AtLeastOnce{T}"/> method to match an element at least once.
/// </summary>
public static class AtLeastOnceExample
{
public static void main()
{
var listOfStringWithOneStringRepeated = new List<string>()
{
"Hello", "World", "Hello", "Hello", "Hello"
};

var atLeastOneHello = Gregex.Is("Hello").AtLeastOnce();

var matcher = new Matcher<string>();

var matches = matcher.FindMatches(atLeastOneHello, listOfStringWithOneStringRepeated);

Console.WriteLine(string.Join(Environment.NewLine, matches));
}
}
31 changes: 31 additions & 0 deletions examples/Anexia.Gregex.Examples/FollowedByExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ------------------------------------------------------------------------------------------
// <copyright file = "FollowedByExample.cs" company = "ANEXIA® Internetdienstleistungs GmbH">
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------------------

namespace Anexia.Gregex.Examples;

/// <summary>
/// Example of how to use the <see cref="Gregex.FollowedBy{T}"/> method to match to consecutive elements.
/// </summary>
public static class FollowedByExample
{
public static void main()
{
var listOfWords = new List<string>
{
"This", "is", "a", "test", "of", "Gregex", "followed", "by", "a",
"example", "one", "more", "time", "and", "then",
"we'll", "see", "if", "it's", "working", "This", "is", "not", "a", "test"
};

var aTest = Gregex.Is("a").FollowedBy(Gregex.Is("test"));

var matcher = new Matcher<string>();

var matches = matcher.FindMatches(aTest, listOfWords);

Console.WriteLine(string.Join(Environment.NewLine, matches));
}
}
43 changes: 43 additions & 0 deletions examples/Anexia.Gregex.Examples/IsExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ------------------------------------------------------------------------------------------
// <copyright file = "IsExample.cs" company = "ANEXIA® Internetdienstleistungs GmbH">
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------------------

namespace Anexia.Gregex.Examples;

/// <summary>
/// Example for matching an element exactly using the <see cref="Gregex.Is{T}"/> method.
/// </summary>
public static class IsExample
{
public static void main()
{
var listOfWords = new List<string>()
{
"Hello",
"World",
"This",
"Is",
"And",
"Test",
"Example",
"List",
"With",
"More",
"Words",
"Like",
"These",
"And",
"Those"
};

var gregex = Gregex.Is("And");

var matcher = new Matcher<string>();

var matches = matcher.FindMatches(gregex, listOfWords);

Console.WriteLine(string.Join(Environment.NewLine, matches));
}
}
42 changes: 6 additions & 36 deletions examples/Anexia.Gregex.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
using Anexia.Gregex;
using Anexia.Gregex.Examples;

var fooBar = "FooBarFooBarFoo".ToCharArray();

var twoOs = Gregex.Is('o').Times(2);

var anyChar = Gregex.Any<char>();

var matcher = new Matcher<char>();

var oMatches = matcher.FindMatches(twoOs, fooBar).ToArray();

Console.WriteLine($"Found: {oMatches.Length} os.");
Console.WriteLine(string.Join("\n", oMatches.AsEnumerable()));

var anyMatches = matcher.FindMatches(anyChar, fooBar).ToArray();

Console.WriteLine($"Found: {anyMatches.Length} matches for any.");
Console.WriteLine(string.Join("\n", anyMatches.AsEnumerable()));

var anyAtLeastOnce = Gregex.Is('a').AtLeastOnce();

var anyAtLeastOnceMatches = matcher.FindMatches(anyAtLeastOnce, ['a', 'a', 'a']).ToArray();
Console.WriteLine($"Found: {anyAtLeastOnceMatches.Length} matches for at least one any.");
Console.WriteLine(string.Join("\n", anyAtLeastOnceMatches.AsEnumerable()));

var oFollowedByB = Gregex.Is('o').FollowedBy(Gregex.Is('B'));

var oFollowedByBMatches = matcher.FindMatches(oFollowedByB, fooBar).ToArray();
Console.WriteLine($"Found: {oFollowedByBMatches.Length} matches for oB.");
Console.WriteLine(string.Join("\n", oFollowedByBMatches.AsEnumerable()));

var anyThinkBeforeF = Gregex.Pattern(Gregex.Any<char>(), Gregex.Is('F'));

var anyThinkBeforeFMatches = matcher.FindMatches(anyThinkBeforeF, fooBar).ToArray();
Console.WriteLine($"Found: {anyThinkBeforeFMatches.Length} matches for *F.");
Console.WriteLine(string.Join("\n", anyThinkBeforeFMatches.AsEnumerable()));
AnyExample.main();
IsExample.main();
TimesExample.main();
AtLeastOnceExample.main();
FollowedByExample.main();
29 changes: 29 additions & 0 deletions examples/Anexia.Gregex.Examples/TimesExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ------------------------------------------------------------------------------------------
// <copyright file = "TimesExample.cs" company = "ANEXIA® Internetdienstleistungs GmbH">
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------------------

namespace Anexia.Gregex.Examples;

/// <summary>
/// Example of how to use the <see cref="Gregex.Times{T}"/> method to match an element multiple times.
/// </summary>
public static class TimesExample
{
public static void main()
{
var listOfStringWithOneStringRepeated = new List<string>()
{
"Hello", "World", "Hello", "Hello", "Hello"
};

var twoTimesHello = Gregex.Is("Hello").Times(2);

var matcher = new Matcher<string>();

var matches = matcher.FindMatches(twoTimesHello, listOfStringWithOneStringRepeated);

Console.WriteLine(string.Join(Environment.NewLine, matches));
}
}

0 comments on commit a241aa8

Please sign in to comment.