From 7e582b56c5001f40a2876e0b9f63b095ee7ab93d Mon Sep 17 00:00:00 2001 From: Yommy Date: Mon, 23 Mar 2020 11:37:36 +1000 Subject: [PATCH] Added support for RegexOptions - dot matches line breaks - case sensitive matching --- OpenBullet/LSDoc.xml | 2 +- OpenBullet/Views/StackerBlocks/PageBlockParse.xaml | 4 ++++ RuriLib/Blocks/BlockParse.cs | 14 ++++++++++++-- RuriLib/Functions/Parsing/Parse.cs | 13 ++++++++++--- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/OpenBullet/LSDoc.xml b/OpenBullet/LSDoc.xml index b469c2b..c7c833b 100644 --- a/OpenBullet/LSDoc.xml +++ b/OpenBullet/LSDoc.xml @@ -325,7 +325,7 @@ Syntax: PARSE "TARGET" LR "LEFT" "RIGHT" [Recursive?] [EncodeOutput?] [CreateEmpty?] [UseRegexLR?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] PARSE "TARGET" CSS "SELECTOR" "ATTRIBUTE" [INDEX / Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] PARSE "TARGET" JSON "FIELD" [JTokenParsing?] [Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] -PARSE "TARGET" REGEX "REGEX" "OUTPUT" [Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" REGEX "REGEX" "OUTPUT" [Recursive?] [EncodeOutput?] [CreateEmpty?] [DotMatches?] [CaseSensitive?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] Examples: PARSE "<SOURCE>" LR "<span>" "</span>" Recursive=True -> CAP "BALANCE" "$" "" diff --git a/OpenBullet/Views/StackerBlocks/PageBlockParse.xaml b/OpenBullet/Views/StackerBlocks/PageBlockParse.xaml index fb145cb..3d3afec 100644 --- a/OpenBullet/Views/StackerBlocks/PageBlockParse.xaml +++ b/OpenBullet/Views/StackerBlocks/PageBlockParse.xaml @@ -148,6 +148,10 @@ + + + + diff --git a/RuriLib/Blocks/BlockParse.cs b/RuriLib/Blocks/BlockParse.cs index 6e376d6..d0c7ac8 100644 --- a/RuriLib/Blocks/BlockParse.cs +++ b/RuriLib/Blocks/BlockParse.cs @@ -61,6 +61,14 @@ namespace RuriLib /// Whether to parse multiple values that match the criteria or just the first one. public bool Recursive { get { return recursive; } set { recursive = value; OnPropertyChanged(); } } + private bool dotMatches = false; + /// Whether Regex . matches over multiple lines. + public bool DotMatches { get { return dotMatches; } set { dotMatches = value; OnPropertyChanged(); } } + + private bool caseSensitive = true; + /// Whether Regex matches are case sensitive. + public bool CaseSensitive { get { return caseSensitive; } set { caseSensitive = value; OnPropertyChanged(); } } + private bool encodeOutput = false; /// Whether to URL encode the parsed text. 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; } diff --git a/RuriLib/Functions/Parsing/Parse.cs b/RuriLib/Functions/Parsing/Parse.cs index ede1b2b..fdd0000 100644 --- a/RuriLib/Functions/Parsing/Parse.cs +++ b/RuriLib/Functions/Parsing/Parse.cs @@ -241,14 +241,21 @@ namespace RuriLib.Utils.Parsing /// The Regex pattern containing groups /// The output format string, for which [0] will be replaced with the full match, [1] with the first group etc. /// Whether to make multiple matches + /// Whether Regex . matches over multiple lines + /// Whether Regex is case sensitive /// The parsed string(s). - public static IEnumerable REGEX(string input, string pattern, string output, bool recursive = false) + public static IEnumerable REGEX(string input, string pattern, string output, bool recursive = false, bool dotMatches = false, bool caseSensitive = true) { var list = new List(); + 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;