Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public IList<string> PythonRegex(
- string needle,
- string haystack,
- bool enhanceMatch = false,
- bool bestMatch = false)
- {
- var argNeedle = needle.Replace("'", @"\'");
- var argHaystack = haystack.Replace("'", @"\'");
- var pyCommand = (
- $"import regex;" +
- $"import json;" +
- $"t=regex.search('{argNeedle}','{argHaystack}'," +
- (enhanceMatch ? "regex.ENHANCEMATCH," : "") +
- (bestMatch ? "regex.BESTMATCH," : "") +
- $");" +
- $"print(t.group(0));" +
- $"print(json.dumps(t.groups()));"
- );
- ProcessStartInfo pyStart = new ProcessStartInfo
- {
- FileName = @"python",
- Arguments = $"-c \"{pyCommand}\"",
- UseShellExecute = false,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- };
- string[] groups = null;
- using (var pyProcess = Process.Start(pyStart))
- using (var reader = pyProcess.StandardOutput)
- using (var error = pyProcess.StandardError)
- {
- var err = error.ReadToEnd();
- if (!String.IsNullOrWhiteSpace(err))
- {
- throw new ArgumentException(err);
- }
- var first = reader.ReadLine();
- var others = reader.ReadLine();
- groups = new object[] { first }
- .Concat(JArray.Parse(others))
- .Select(t => t.ToString())
- .ToArray();
- }
- return groups;
- }
Advertisement
Add Comment
Please, Sign In to add comment