Added support for RegexOptions

- dot matches line breaks
- case sensitive matching
This commit is contained in:
Yommy
2020-03-23 11:37:36 +10:00
parent a7fd74a781
commit 7e582b56c5
4 changed files with 27 additions and 6 deletions
+12 -2
View File
@@ -61,6 +61,14 @@ namespace RuriLib
/// <summary>Whether to parse multiple values that match the criteria or just the first one.</summary>
public bool Recursive { get { return recursive; } set { recursive = value; OnPropertyChanged(); } }
private bool dotMatches = false;
/// <summary>Whether Regex . matches over multiple lines.</summary>
public bool DotMatches { get { return dotMatches; } set { dotMatches = value; OnPropertyChanged(); } }
private bool caseSensitive = true;
/// <summary>Whether Regex matches are case sensitive.</summary>
public bool CaseSensitive { get { return caseSensitive; } set { caseSensitive = value; OnPropertyChanged(); } }
private bool encodeOutput = false;
/// <summary>Whether to URL encode the parsed text.</summary>
public bool EncodeOutput { get { return encodeOutput; } set { encodeOutput = value; OnPropertyChanged(); } }
@@ -257,7 +265,9 @@ namespace RuriLib
.Literal(RegexOutput)
.Boolean(Recursive, "Recursive")
.Boolean(EncodeOutput, "EncodeOutput")
.Boolean(CreateEmpty, "CreateEmpty");
.Boolean(CreateEmpty, "CreateEmpty")
.Boolean(DotMatches, "DotMatches")
.Boolean(CaseSensitive, "CaseSensitive");
break;
}
@@ -299,7 +309,7 @@ namespace RuriLib
throw new NotImplementedException("XPATH parsing is not implemented yet");
case ParseType.REGEX:
list = Parse.REGEX(original, ReplaceValues(regexString, data), ReplaceValues(regexOutput, data), recursive).ToList();
list = Parse.REGEX(original, ReplaceValues(regexString, data), ReplaceValues(regexOutput, data), recursive, dotMatches, caseSensitive).ToList();
break;
}
+10 -3
View File
@@ -241,14 +241,21 @@ namespace RuriLib.Utils.Parsing
/// <param name="pattern">The Regex pattern containing groups</param>
/// <param name="output">The output format string, for which [0] will be replaced with the full match, [1] with the first group etc.</param>
/// <param name="recursive">Whether to make multiple matches</param>
/// <param name="dotMatches">Whether Regex . matches over multiple lines</param>
/// <param name="caseSensitive">Whether Regex is case sensitive</param>
/// <returns>The parsed string(s).</returns>
public static IEnumerable<string> REGEX(string input, string pattern, string output, bool recursive = false)
public static IEnumerable<string> REGEX(string input, string pattern, string output, bool recursive = false, bool dotMatches = false, bool caseSensitive = true)
{
var list = new List<string>();
RegexOptions options = new RegexOptions();
if (dotMatches)
options |= RegexOptions.Singleline;
if (caseSensitive == false)
options |= RegexOptions.IgnoreCase;
if (recursive)
{
var matches = Regex.Matches(input, pattern);
var matches = Regex.Matches(input, pattern, options);
foreach (Match match in matches)
{
var final = output;
@@ -258,7 +265,7 @@ namespace RuriLib.Utils.Parsing
}
else
{
var match = Regex.Match(input, pattern);
var match = Regex.Match(input, pattern, options);
if (match.Success)
{
var final = output;