mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 19:22:53 +01:00
RegexUtils] - move regex methods to regexutils.
This commit is contained in:
parent
c520cc8e28
commit
9950d9499b
@ -132,7 +132,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
|
||||
{
|
||||
if (node.Attributes["find"] != null && node.Attributes["replaceWith"] != null)
|
||||
{
|
||||
return Utilities.IsValidRegex(node.Attributes["find"].Value);
|
||||
return RegexUtils.IsValidRegex(node.Attributes["find"].Value);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -17,7 +17,7 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
|
||||
string s = p.Text;
|
||||
if (s.Contains('i'))
|
||||
{
|
||||
s = FixAloneLowercaseIToUppercaseLine(SubtitleEditRegex.LittleIRegex, oldText, s, 'i');
|
||||
s = FixAloneLowercaseIToUppercaseLine(RegexUtils.LittleIRegex, oldText, s, 'i');
|
||||
if (s != oldText && callbacks.AllowFix(p, fixAction))
|
||||
{
|
||||
p.Text = s;
|
||||
|
@ -17,15 +17,15 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
|
||||
string oldText = text;
|
||||
|
||||
// Make sure text contains lower: 'i'.
|
||||
if (SubtitleEditRegex.LittleIRegex.IsMatch(text))
|
||||
if (RegexUtils.LittleIRegex.IsMatch(text))
|
||||
{
|
||||
foreach (var regex in SubtitleEditRegex.DanishLetterI.DanishCompiledRegexList)
|
||||
foreach (var regex in RegexUtils.DanishLetterI.DanishCompiledRegexList)
|
||||
{
|
||||
Match match = regex.Match(text);
|
||||
while (match.Success)
|
||||
{
|
||||
// Get lower 'i' index from matched value.
|
||||
int iIdx = SubtitleEditRegex.LittleIRegex.Match(match.Value).Index;
|
||||
int iIdx = RegexUtils.LittleIRegex.Match(match.Value).Index;
|
||||
// Remove 'i' from given index and insert new uppwercase 'I'.
|
||||
string temp = match.Value.Remove(iIdx, 1).Insert(iIdx, "I");
|
||||
|
||||
@ -39,26 +39,26 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
|
||||
}
|
||||
}
|
||||
|
||||
if (SubtitleEditRegex.DanishLetterI.RegExIDag.IsMatch(text))
|
||||
text = SubtitleEditRegex.DanishLetterI.RegExIDag.Replace(text, "i dag");
|
||||
if (RegexUtils.DanishLetterI.RegExIDag.IsMatch(text))
|
||||
text = RegexUtils.DanishLetterI.RegExIDag.Replace(text, "i dag");
|
||||
|
||||
if (SubtitleEditRegex.DanishLetterI.RegExIGaar.IsMatch(text))
|
||||
text = SubtitleEditRegex.DanishLetterI.RegExIGaar.Replace(text, "i går");
|
||||
if (RegexUtils.DanishLetterI.RegExIGaar.IsMatch(text))
|
||||
text = RegexUtils.DanishLetterI.RegExIGaar.Replace(text, "i går");
|
||||
|
||||
if (SubtitleEditRegex.DanishLetterI.RegExIMorgen.IsMatch(text))
|
||||
text = SubtitleEditRegex.DanishLetterI.RegExIMorgen.Replace(text, "i morgen");
|
||||
if (RegexUtils.DanishLetterI.RegExIMorgen.IsMatch(text))
|
||||
text = RegexUtils.DanishLetterI.RegExIMorgen.Replace(text, "i morgen");
|
||||
|
||||
if (SubtitleEditRegex.DanishLetterI.RegExIAlt.IsMatch(text))
|
||||
text = SubtitleEditRegex.DanishLetterI.RegExIAlt.Replace(text, "i alt");
|
||||
if (RegexUtils.DanishLetterI.RegExIAlt.IsMatch(text))
|
||||
text = RegexUtils.DanishLetterI.RegExIAlt.Replace(text, "i alt");
|
||||
|
||||
if (SubtitleEditRegex.DanishLetterI.RegExIGang.IsMatch(text))
|
||||
text = SubtitleEditRegex.DanishLetterI.RegExIGang.Replace(text, "i gang");
|
||||
if (RegexUtils.DanishLetterI.RegExIGang.IsMatch(text))
|
||||
text = RegexUtils.DanishLetterI.RegExIGang.Replace(text, "i gang");
|
||||
|
||||
if (SubtitleEditRegex.DanishLetterI.RegExIStand.IsMatch(text))
|
||||
text = SubtitleEditRegex.DanishLetterI.RegExIStand.Replace(text, "i stand");
|
||||
if (RegexUtils.DanishLetterI.RegExIStand.IsMatch(text))
|
||||
text = RegexUtils.DanishLetterI.RegExIStand.Replace(text, "i stand");
|
||||
|
||||
if (SubtitleEditRegex.DanishLetterI.RegExIOevrigt.IsMatch(text))
|
||||
text = SubtitleEditRegex.DanishLetterI.RegExIOevrigt.Replace(text, "i øvrigt");
|
||||
if (RegexUtils.DanishLetterI.RegExIOevrigt.IsMatch(text))
|
||||
text = RegexUtils.DanishLetterI.RegExIOevrigt.Replace(text, "i øvrigt");
|
||||
|
||||
if (text != oldText && callbacks.AllowFix(p, fixAction))
|
||||
{
|
||||
|
@ -226,7 +226,7 @@
|
||||
<Compile Include="SsaStyle.cs" />
|
||||
<Compile Include="StringExtensions.cs" />
|
||||
<Compile Include="StrippableText.cs" />
|
||||
<Compile Include="SubtitleEditRegex.cs" />
|
||||
<Compile Include="RegexUtils.cs" />
|
||||
<Compile Include="Subtitle.cs" />
|
||||
<Compile Include="SubtitleFormats\AribB36.cs" />
|
||||
<Compile Include="SubtitleFormats\AribB24Decoder.cs" />
|
||||
|
@ -1,9 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Core
|
||||
{
|
||||
public static class SubtitleEditRegex
|
||||
public static class RegexUtils
|
||||
{
|
||||
// Others classes may want to use this regex.
|
||||
public static readonly Regex LittleIRegex = new Regex(@"\bi\b", RegexOptions.Compiled);
|
||||
@ -166,5 +167,41 @@ namespace Nikse.SubtitleEdit.Core
|
||||
/// </summary>
|
||||
private static string ExpandWhiteSpace(string pattern) => pattern.Replace(" ", "[ \r\n]+");
|
||||
}
|
||||
|
||||
public static bool IsValidRegex(string testPattern)
|
||||
{
|
||||
if (string.IsNullOrEmpty(testPattern))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
Regex.Match(string.Empty, testPattern);
|
||||
return true;
|
||||
}
|
||||
catch (ArgumentException) // invalid pattern e.g: [
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Regex MakeWordSearchRegex(string word)
|
||||
{
|
||||
string s = word.Replace("\\", "\\\\");
|
||||
s = s.Replace("*", "\\*");
|
||||
s = s.Replace(".", "\\.");
|
||||
s = s.Replace("?", "\\?");
|
||||
return new Regex(@"\b" + s + @"\b", RegexOptions.Compiled);
|
||||
}
|
||||
|
||||
public static Regex MakeWordSearchRegexWithNumbers(string word)
|
||||
{
|
||||
string s = word.Replace("\\", "\\\\");
|
||||
s = s.Replace("*", "\\*");
|
||||
s = s.Replace(".", "\\.");
|
||||
s = s.Replace("?", "\\?");
|
||||
return new Regex(@"[\b ,\.\?\!]" + s + @"[\b !\.,\r\n\?]", RegexOptions.Compiled);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -872,43 +872,6 @@ namespace Nikse.SubtitleEdit.Core
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsValidRegex(string testPattern)
|
||||
{
|
||||
if (string.IsNullOrEmpty(testPattern))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Regex.Match(string.Empty, testPattern);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
// BAD PATTERN: Syntax error
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Regex MakeWordSearchRegex(string word)
|
||||
{
|
||||
string s = word.Replace("\\", "\\\\");
|
||||
s = s.Replace("*", "\\*");
|
||||
s = s.Replace(".", "\\.");
|
||||
s = s.Replace("?", "\\?");
|
||||
return new Regex(@"\b" + s + @"\b", RegexOptions.Compiled);
|
||||
}
|
||||
|
||||
public static Regex MakeWordSearchRegexWithNumbers(string word)
|
||||
{
|
||||
string s = word.Replace("\\", "\\\\");
|
||||
s = s.Replace("*", "\\*");
|
||||
s = s.Replace(".", "\\.");
|
||||
s = s.Replace("?", "\\?");
|
||||
return new Regex(@"[\b ,\.\?\!]" + s + @"[\b !\.,\r\n\?]", RegexOptions.Compiled);
|
||||
}
|
||||
|
||||
public static void RemoveFromUserDictionary(string word, string languageName)
|
||||
{
|
||||
word = word.Trim();
|
||||
|
@ -152,7 +152,7 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Utilities.IsValidRegex(textBoxNoBreakAfter.Text))
|
||||
if (!RegexUtils.IsValidRegex(textBoxNoBreakAfter.Text))
|
||||
{
|
||||
MessageBox.Show(Configuration.Settings.Language.General.RegularExpressionIsNotValid);
|
||||
return;
|
||||
|
@ -197,7 +197,7 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
else if (radioButtonRegEx.Checked)
|
||||
{
|
||||
searchType = SearchTypeRegularExpression;
|
||||
if (!Utilities.IsValidRegex(findText))
|
||||
if (!RegexUtils.IsValidRegex(findText))
|
||||
{
|
||||
MessageBox.Show(Configuration.Settings.Language.General.RegularExpressionIsNotValid);
|
||||
textBoxFind.Select();
|
||||
@ -424,7 +424,7 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
else if (radioButtonRegEx.Checked)
|
||||
{
|
||||
searchType = SearchTypeRegularExpression;
|
||||
if (!Utilities.IsValidRegex(findText))
|
||||
if (!RegexUtils.IsValidRegex(findText))
|
||||
{
|
||||
MessageBox.Show(Configuration.Settings.Language.General.RegularExpressionIsNotValid);
|
||||
textBoxFind.Select();
|
||||
|
@ -253,7 +253,7 @@ namespace Nikse.SubtitleEdit.Logic
|
||||
// validate pattern if find type is regex
|
||||
if (FindReplaceType.FindType == FindType.RegEx)
|
||||
{
|
||||
if (!Utilities.IsValidRegex(_findText))
|
||||
if (!RegexUtils.IsValidRegex(_findText))
|
||||
{
|
||||
MessageBox.Show(Configuration.Settings.Language.General.RegularExpressionIsNotValid);
|
||||
return count;
|
||||
|
@ -450,7 +450,7 @@ namespace Nikse.SubtitleEdit.Logic.Ocr
|
||||
if (SpellCheckDictionaryName.StartsWith("en_", StringComparison.Ordinal) || _threeLetterIsoLanguageName == "eng")
|
||||
{
|
||||
string oldText = text;
|
||||
text = FixAloneLowercaseIToUppercaseI.FixAloneLowercaseIToUppercaseLine(SubtitleEditRegex.LittleIRegex, oldText, text, 'i');
|
||||
text = FixAloneLowercaseIToUppercaseI.FixAloneLowercaseIToUppercaseLine(RegexUtils.LittleIRegex, oldText, text, 'i');
|
||||
text = FixAloneLowercaseIToUppercaseI.FixAloneLowercaseIToUppercaseLine(RegexAloneIasL, oldText, text, 'l');
|
||||
}
|
||||
else if (_threeLetterIsoLanguageName == "fra")
|
||||
|
@ -497,19 +497,19 @@ namespace Test.Logic
|
||||
[TestMethod]
|
||||
public void IsValidRegexOk1()
|
||||
{
|
||||
Assert.IsTrue(Utilities.IsValidRegex(@"^(?![\s\S])"));
|
||||
Assert.IsTrue(RegexUtils.IsValidRegex(@"^(?![\s\S])"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsValidRegexOk2()
|
||||
{
|
||||
Assert.IsTrue(Utilities.IsValidRegex(@"\d+"));
|
||||
Assert.IsTrue(RegexUtils.IsValidRegex(@"\d+"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsValidRegexBad1()
|
||||
{
|
||||
Assert.IsFalse(Utilities.IsValidRegex(@"[\s\S(\()()(()\)"));
|
||||
Assert.IsFalse(RegexUtils.IsValidRegex(@"[\s\S(\()()(()\)"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
Loading…
Reference in New Issue
Block a user