/// <summary> /// Deals with the odd path like ..\NotNeeded\..\ActualFolder\somefile.txt /// and ..\Somewhere\.\Andthen\somefile.txt /// which is technically valid. /// </summary> /// <param name="somePath"></param> /// <returns></returns> public static string RemoveRedundantPathLeafs(string somePath) { if (String.IsNullOrWhiteSpace(somePath)) { return(null); } const string PATH_SEP = @"\x5c\x2f"; const string DOT_DOT = @"\x2e\x2e"; var invalidPathChars = new string(Path.GetInvalidPathChars()).EscapeString(); //anywhere we find a pattern like ..\AnyNameHere\.. var regexPattern = "(" + DOT_DOT + "[" + PATH_SEP + "]" + "[^" + invalidPathChars + DOT_DOT.EscapeString() + "]+?" + "[" + PATH_SEP + "])" + DOT_DOT; string matchText; if (RegexCatalog.IsRegexMatch(somePath, regexPattern, out matchText, 1) && !String.Equals(@"..\..\", matchText)) { somePath = somePath.Replace(matchText, String.Empty); } somePath = somePath.Replace(@"\.\", @"\").Replace("/./", "/"); return(somePath); }
public void TestAppropriateAllRegex() { var testResultOut = _hardCodedIpV4; RegexCatalog.AppropriateAllRegex(ref testResultOut, Regex2Values); Assert.AreEqual(Regex2Values[_hardCodedIpV4], testResultOut); }
private void WriteEndpointAddressClosed(Hashtable regex2Values, XmlElement endPtNode) { var endPtAddrAttr = endPtNode.Attributes["address"]; //implies the a contract with no end point address whatsoever. if (string.IsNullOrWhiteSpace(endPtAddrAttr?.Value)) { return; } AddEndpointNodeToTransform( endPtAddrAttr.Value, endPtNode.Attributes["binding"]?.Value, endPtNode.Attributes["bindingConfiguration"]?.Value, endPtNode.Attributes["contract"]?.Value, endPtNode.Attributes["name"]?.Value); if (!RegexCatalog.AreAnyRegexMatch(endPtAddrAttr.Value, regex2Values.Keys.Cast <string>().ToArray())) { var addUri = new UriBuilder(endPtAddrAttr.Value) { Host = "localhost" }; endPtAddrAttr.Value = addUri.ToString(); } else { AppropriateAllRegex(endPtAddrAttr, regex2Values); } }
internal static Tuple <SecForm, string> ParseNameFromTitle(string titleText) { var blank = new Tuple <SecForm, string>(new Form10K(), String.Empty); if (String.IsNullOrWhiteSpace(titleText)) { return(blank); } if (!titleText.Contains("-")) { return(blank); } string name; if (!RegexCatalog.IsRegexMatch(titleText, @"(.*)?\x20\x2D\x20(.*)?", out name, 2)) { return(blank); } string formAbbrev; RegexCatalog.IsRegexMatch(titleText, @"(.*)?\x20\x2D\x20(.*)?", out formAbbrev, 1); var secForm = SecForm.SecFormFactory(formAbbrev) ?? new Form10K(); return(new Tuple <SecForm, string>(secForm, (name ?? String.Empty).Trim())); }
/// <summary> /// A recursive function to turn the place-holders of <see cref="value"/> /// into their fully expanded form. /// </summary> /// <param name="idValueHash"></param> /// <param name="value"></param> /// <returns></returns> /// <remarks> /// See the xml comment atop of <see cref="FILE_NAME"/> for an explanation /// of what a placeholder is and how it works. /// </remarks> internal static string ExpandCfgValue(Dictionary <string, string> idValueHash, string value) { var xRefId = ""; if (!RegexCatalog.IsRegexMatch(value, @"\x24\x28([a-zA-Z0-9_\-\x25\x28\x29]+)\x29", out xRefId, 1)) { return(value); } var valueLessXrefId = value.Replace($"$({xRefId})", ""); if (xRefId.StartsWith("%") && xRefId.EndsWith("%")) { xRefId = Environment.ExpandEnvironmentVariables(xRefId); return($"{xRefId}{valueLessXrefId}"); } if (!idValueHash.ContainsKey(xRefId)) { return($"{Environment.CurrentDirectory}{valueLessXrefId}"); } if (idValueHash[xRefId].Contains("$")) { var rValue = ExpandCfgValue(idValueHash, idValueHash[xRefId]); return($"{rValue}{valueLessXrefId}"); } return($"{idValueHash[xRefId]}{valueLessXrefId}"); }
/// <summary> /// Returns everything except the last entry after <see cref="NfSettings.DefaultTypeSeparator"/> /// or in the case where <see cref="NfSettings.DefaultTypeSeparator"/> isn't present - /// just returns <see cref="name"/> /// </summary> /// <param name="name"></param> /// <returns></returns> public static string GetNamespaceWithoutTypeName(string name) { const string ROOT_NS = "global::"; if (String.IsNullOrWhiteSpace(name)) { return(null); } if (!name.Contains(NfSettings.DefaultTypeSeparator)) { return(null); } if (name.StartsWith(ROOT_NS)) { name = name.Substring(ROOT_NS.Length); } string genericCount; if (RegexCatalog.IsRegexMatch(name, @"\x60([1-9])\x5B", out genericCount)) { name = name.Substring(0, name.IndexOf("`", StringComparison.Ordinal)); } if (name.Contains(Constants.TYPE_METHOD_NAME_SPLIT_ON)) { name = name.Substring(0, name.IndexOf(Constants.TYPE_METHOD_NAME_SPLIT_ON, StringComparison.Ordinal)); } var nameArray = name.Split(NfSettings.DefaultTypeSeparator); var nsLength = nameArray.Length - 1; if (nsLength < 0) { return(name); } var ns = new StringBuilder(); for (var i = 0; i < nsLength; i++) { var s = nameArray[i]; if (String.IsNullOrWhiteSpace(s)) { continue; } ns.Append(s); //as long as its not the last entry if (i < nsLength - 1) { ns.Append(NfSettings.DefaultTypeSeparator); } } return(ns.ToString()); }
public void TestToRegexExpression() { var testInput = "Dependent (18 yrs +)"; var testResult = RegexCatalog.ToRegexExpression(testInput); Assert.IsNotNull(testResult); System.Text.RegularExpressions.Regex.IsMatch("Dependents", testResult); Console.WriteLine(testResult); }
public TickerComparer(string companyName) { if (string.IsNullOrWhiteSpace(companyName)) { throw new ArgumentNullException(nameof(companyName)); } _companyName = companyName; _companyNameRegex = new Regex(RegexCatalog.ToRegexExpression(_companyName), RegexOptions.IgnoreCase); }
internal static IEnumerable <string> RemoveOrphanedDecorators(IEnumerable <string> lines, IEnumerable <int> atLines) { var srcLines = lines?.ToArray(); if (srcLines == null || srcLines.Length <= 0) { return(srcLines); } var decoratorRegexPattern = Settings.LangStyle.GetDecoratorRegex(); var openEncl = Settings.LangStyle.GetEnclosureOpenToken(null); var closeEncl = Settings.LangStyle.GetEnclosureCloseToken(null); if (string.IsNullOrWhiteSpace(decoratorRegexPattern)) { return(srcLines); } decoratorRegexPattern = "(" + decoratorRegexPattern + ")"; var totalLen = srcLines.Length; var lineNums = atLines.OrderBy(v => v).ToArray(); for (var i = 0; i < lineNums.Length; i++) { var startLnIdx = lineNums[i]; var prevLnIdx = i == 0 ? -1 : lineNums[i - 1]; //need to remove all decorators from here where abs min is previous target for (var sLnNum = startLnIdx; sLnNum > prevLnIdx; sLnNum--) { var sLn = sLnNum < 0 || sLnNum >= totalLen ? string.Empty : srcLines[sLnNum]; //break out on any line having an open or close enclosure token bool myRef = false; var sLnTrim = Settings.LangStyle.EscStringLiterals(sLn.Trim(), EscapeStringType.BLANK, ref myRef); if (sLnNum != startLnIdx && (sLnTrim.Contains(openEncl) || sLnTrim.Contains(closeEncl))) { break; } if (RegexCatalog.IsRegexMatch(sLn, decoratorRegexPattern, out var decorator)) { srcLines[sLnNum] = sLn.Replace(decorator, new string(' ', decorator.Length)); } } } return(srcLines); }
public void TestAreAnyRegexMatch() { var testResult = RegexCatalog.AreAnyRegexMatch("www.TheDomainName.com", Regex2Values.Keys.Cast <string>().ToArray()); Assert.IsTrue(testResult); testResult = RegexCatalog.AreAnyRegexMatch("net.tcp://224.11.89.55:1060/OrNotToWankAdminUIController/", Regex2Values.Keys.Cast <string>().ToArray()); Assert.IsTrue(testResult); testResult = RegexCatalog.AreAnyRegexMatch("ContractDocument", new[] { "Contract", "Client", "Location" }); Assert.IsTrue(testResult); }
internal void SplitClientServiceModelNodes(Hashtable regex2Values) { var svcClientNode = _xmlDocument.SelectSingleNode("//system.serviceModel/client"); if (svcClientNode == null || !svcClientNode.HasChildNodes) { return; } _xmlWriter.WriteStartElement("client"); foreach (var node in svcClientNode.ChildNodes) { var endPtNode = node as XmlElement; var addrAttr = endPtNode?.Attributes["address"]; if (addrAttr == null) { continue; } if (!Uri.IsWellFormedUriString(addrAttr.Value, UriKind.RelativeOrAbsolute)) { continue; } AddEndpointNodeToTransform( endPtNode.Attributes["address"]?.Value, endPtNode.Attributes["binding"]?.Value, endPtNode.Attributes["bindingConfiguration"]?.Value, endPtNode.Attributes["contract"]?.Value, endPtNode.Attributes["name"]?.Value); if (!RegexCatalog.AreAnyRegexMatch(addrAttr.Value, regex2Values.Keys.Cast <string>().ToArray())) { var addUri = new UriBuilder(addrAttr.Value) { Host = "localhost" }; addrAttr.Value = addUri.ToString(); } else { AppropriateAllRegex(addrAttr, regex2Values); } } _xmlWriter.WriteEndElement(); }
internal static void AppropriateAllRegex(XmlAttribute attr, Hashtable regex2Values) { var newAppVal = attr?.Value; if (string.IsNullOrEmpty(newAppVal)) { return; } if (regex2Values == null || regex2Values.Count <= 0) { return; } RegexCatalog.AppropriateAllRegex(ref newAppVal, regex2Values); attr.Value = newAppVal; }
internal void SplitAppSettings(Hashtable regex2Values, bool swapConnStrs) { var appSettingsNodes = _xmlDocument.SelectSingleNode("//appSettings"); if (appSettingsNodes == null || !appSettingsNodes.HasChildNodes) { return; } _xmlWriter.WriteStartElement("appSettings"); foreach (var node in appSettingsNodes) { var cnode = node as XmlElement; if (cnode == null) { continue; } var appValAttr = cnode.Attributes["value"]; var appKeyAttr = cnode.Attributes["key"]; if (appValAttr == null || appKeyAttr == null) { continue; } var originalVal = appValAttr.Value; if (swapConnStrs && IsConnectionString(appValAttr.Value)) { AddAppSettingAddNodeToTransform(appKeyAttr.Value, appValAttr.Value); appValAttr.Value = NfConfig.SqlServerDotNetConnString; continue; } if (!RegexCatalog.AreAnyRegexMatch(appValAttr.Value, regex2Values.Keys.Cast <string>().ToArray())) { continue; } AppropriateAllRegex(appValAttr, regex2Values); AddAppSettingAddNodeToTransform(appKeyAttr.Value, originalVal); } _xmlWriter.WriteEndElement(); }
private void WriteServiceHostNodeClosed(Hashtable regex2Values, XmlNode hostNode) { _xmlWriter.WriteStartElement("host"); _xmlWriter.WriteAttributeString("Transform", XML_TRANSFORM_NS, "Replace"); _xmlWriter.WriteStartElement("baseAddresses"); foreach (var nodeV in hostNode.ChildNodes) { var bAddrNode = nodeV as XmlElement; if (bAddrNode == null) { continue; } var baseAddrAttr = bAddrNode.Attributes["baseAddress"]; if (string.IsNullOrEmpty(baseAddrAttr?.Value) || !Uri.IsWellFormedUriString(baseAddrAttr.Value, UriKind.RelativeOrAbsolute)) { _xmlWriter.WriteEndElement(); _xmlWriter.WriteEndElement(); continue; } _xmlWriter.WriteStartElement("add"); _xmlWriter.WriteAttributeString("baseAddress", baseAddrAttr.Value); if (!RegexCatalog.AreAnyRegexMatch(baseAddrAttr.Value, regex2Values.Keys.Cast <string>().ToArray())) { var bAddrUri = new UriBuilder(baseAddrAttr.Value) { Host = "localhost" }; baseAddrAttr.Value = bAddrUri.ToString(); } else { AppropriateAllRegex(baseAddrAttr, regex2Values); } _xmlWriter.WriteEndElement(); } _xmlWriter.WriteEndElement(); //end baseAddresses node _xmlWriter.WriteEndElement(); //end host node }
internal void SplitConnectionStringNodes(Hashtable regex2Values, bool swapConnStrs) { var connectionStringNodes = _xmlDocument.SelectSingleNode("//connectionStrings"); if (connectionStringNodes == null || !connectionStringNodes.HasChildNodes) { return; } _xmlWriter.WriteStartElement("connectionStrings"); foreach (var node in connectionStringNodes.ChildNodes) { var constr = node as XmlElement; if (constr == null) { continue; } var conStrAttr = constr.Attributes["connectionString"]; var conStrNameAttr = constr.Attributes["name"]; if (conStrAttr == null) { continue; } var conStrVal = conStrAttr.Value; if (swapConnStrs || !RegexCatalog.AreAnyRegexMatch(conStrAttr.Value, regex2Values.Keys.Cast <string>().ToArray())) { conStrAttr.Value = NfConfig.SqlServerDotNetConnString; AddConnStringNodeToTransform(conStrNameAttr.Value, conStrVal); continue; } AppropriateAllRegex(conStrAttr, regex2Values); AddConnStringNodeToTransform(conStrNameAttr.Value, conStrVal); } _xmlWriter.WriteEndElement(); }
public void TestIsRegexMatch() { string testResultOut; var testResult = RegexCatalog.IsRegexMatch("10.130.55.36", _testSubject.IPv4, out testResultOut); Assert.IsTrue(testResult); testResultOut = null; testResult = RegexCatalog.IsRegexMatch("net.tcp://10.130.55.36/MyService.svc", _testSubject.IPv4, out testResultOut); Assert.IsTrue(testResult); Assert.AreEqual("10.130.55.36", testResultOut); testResultOut = null; testResult = RegexCatalog.IsRegexMatch("https://www.TheDomainName.com/MyService.svc", _testSubject.UrlClassicAmerican, out testResultOut); Assert.IsTrue(testResult); Assert.AreEqual("www.TheDomainName.com", testResultOut); }
/// <summary> /// Determines the year for the given <see cref="contextRef"/> within this <see cref="xml"/> doc. /// </summary> /// <param name="contextRef"></param> /// <param name="xml"></param> /// <param name="nsMgr"></param> /// <param name="yyyy"></param> /// <returns></returns> internal static bool TryGetYear(string contextRef, XmlDocument xml, XmlNamespaceManager nsMgr, out int yyyy) { yyyy = 0; if (xml == null) { return(false); } if (string.IsNullOrWhiteSpace(contextRef)) { return(false); } //allow for some kind of fall-back value based on the contextRef text itself if (RegexCatalog.IsRegexMatch(contextRef, DF_YEAR_PARSE_REGEX, out var regexParsed, 1)) { int.TryParse(regexParsed, out yyyy); } //use the contextRef back to the node to which its a ref var contextPeriodEndNode = xml.SelectSingleNode( $"//{XmlNs.ROOTNS}:context[@id='{contextRef}']/{XmlNs.ROOTNS}:period/{XmlNs.ROOTNS}:endDate", nsMgr); if (contextPeriodEndNode == null || !contextPeriodEndNode.HasChildNodes || string.IsNullOrWhiteSpace(contextPeriodEndNode.InnerText)) { return(yyyy > 0); } DateTime date; if (DateTime.TryParse(contextPeriodEndNode.InnerText, out date)) { yyyy = date.Year; } return(yyyy > 0); }
/// <summary> /// Will parse the results of an invocation to 'tList.exe' with the '-c' switch. /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static List <Tuple <int, string, string> > ParseTListExeOutput(string buffer) { const string LN_I = @"([0-9]+)\W(.+?\x2E[Ee][Xx][Ee])\W"; const string LN_IPLUS1 = @"\WCommand\sLine\:\W(.*)"; var procs = new List <Tuple <int, string, string> >(); var stdOut = buffer.Split('\n'); for (var i = 0; i < stdOut.Length; i += 2) { if (string.IsNullOrWhiteSpace(stdOut[i]) || stdOut[i].Split(' ').Length == 0 || i % 2 != 0) { continue; } string pid; string proc; string cmdln; if (!RegexCatalog.IsRegexMatch(stdOut[i], LN_I, out pid, 1)) { pid = "0"; } if (!RegexCatalog.IsRegexMatch(stdOut[i], LN_I, out proc, 2)) { proc = stdOut[i]; } if (!RegexCatalog.IsRegexMatch(stdOut[i + 1], LN_IPLUS1, out cmdln, 1)) { cmdln = stdOut[i + 1]; } int iPid; int.TryParse(pid, out iPid); procs.Add(new Tuple <int, string, string>(iPid, proc.Trim(), cmdln.Trim())); } return(procs); }
public SocialSecurityNumber() { var regexCatalog = new RegexCatalog(); _regexPattern = regexCatalog.SSN; }
public override IEnumerable <dynamic> ParseContent(object content) { DateTime rptDt = DateTime.Today; var lrgBnkLstTxt = content as string; const string TRAILER_LINE_TEXT = "Summary:"; const string LINE_ITEM_REGEX = @"^\x20([A-Z0-9\x20\x26]{27})([0-9\x20]{10})([0-9\x20]{11})([A-Z\x20\x2C]{23})"; if (string.IsNullOrWhiteSpace(lrgBnkLstTxt)) { return(null); } var crlf = new[] { (char)0x0D, (char)0x0A }; var lines = lrgBnkLstTxt.Split(crlf); //find the range for the reports body int startAtLine = 0; int numOfLines = 0; int matchCount = 0; for (var i = 0; i < lines.Length; i++) { var ln = lines[i]; if (string.IsNullOrWhiteSpace(ln)) { continue; } if (ln.ToCharArray().All(c => c == '-' || c == (char)0x20)) { matchCount += 1; } if (matchCount == 2) { startAtLine = i; matchCount = 0; continue; } if (startAtLine > 0) { numOfLines += 1; } if (ln == TRAILER_LINE_TEXT) { break; } } if (numOfLines < 1) { return(null); } var rptBody = new string[numOfLines]; var rptHdr = new string[startAtLine]; Array.Copy(lines, startAtLine, rptBody, 0, numOfLines); Array.Copy(lines, 0, rptHdr, 0, startAtLine); var regexCatalog = new RegexCatalog(); var dtRegex = new System.Text.RegularExpressions.Regex(regexCatalog.LongDate); foreach (var hdrLn in rptHdr) { if (!dtRegex.IsMatch(hdrLn)) { continue; } var dtMatch = dtRegex.Match(hdrLn); var strDt = dtMatch.Groups[0].Captures[0].Value; DateTime.TryParse(strDt, out rptDt); } Func <string[], int, string> getLnVal = (strings, i) => strings.Length >= i ? strings[i] : string.Empty; var dataOut = new List <dynamic>(); var lnRegex = new System.Text.RegularExpressions.Regex(LINE_ITEM_REGEX); foreach (var line in rptBody) { if (!lnRegex.IsMatch(line)) { continue; } var matches = lnRegex.Match(line); var bankName = matches.Groups[1].Captures[0].Value; if (string.IsNullOrWhiteSpace(bankName)) { continue; } var li = SplitLrgBnkListLine(line).ToArray(); dataOut.Add(new { RptDate = rptDt, BankName = getLnVal(li, 0), NatlRank = getLnVal(li, 1), BankId = getLnVal(li, 2), Location = getLnVal(li, 3), Chtr = getLnVal(li, 4), ConsolAssets = getLnVal(li, 5), DomesticAssets = getLnVal(li, 6), DomAsPercentCons = getLnVal(li, 7), CumlAsPercentCons = getLnVal(li, 8), NumOfDomBranches = getLnVal(li, 9), NumOfFgnBranches = getLnVal(li, 10), Ibf = getLnVal(li, 11), PercentFgnOwned = getLnVal(li, 12) }); } return(dataOut); }
public override IEnumerable <dynamic> ParseContent(object content) { var webResponseBody = GetWebResponseBody(content); if (webResponseBody == null) { return(null); } string[] d; if (!Antlr.HTML.HtmlParseResults.TryGetCdata(webResponseBody, null, out d)) { return(null); } var innerText = d.ToList(); if (innerText.Count <= 0) { return(null); } var rt = String.Empty; var nm = String.Empty; var rssd = String.Empty; var fdicCert = String.Empty; var st = innerText.FindIndex( x => x.Trim().StartsWith("Branch Locator")); var fromHere = innerText.Skip(st + 1); foreach (var val in fromHere.Where(x => !x.StartsWith(" "))) { if (RegexCatalog.IsRegexMatch(val.Trim(), @"[a-zA-Z\x2C\x20]+", out nm)) { break; } } st = innerText.FindIndex( x => x.Trim().StartsWith("RSSD ID")); fromHere = innerText.Skip(st); foreach (var val in fromHere) { if (RegexCatalog.IsRegexMatch(val.Trim(), @"^[0-9]+$", out rssd)) { break; } } st = innerText.FindIndex( x => x.Trim().StartsWith("FDIC Certificate")); fromHere = innerText.Skip(st); foreach (var val in fromHere) { if (RegexCatalog.IsRegexMatch(val.Trim(), @"^[0-9]+$", out fdicCert)) { break; } } st = innerText.FindIndex( x => x.Trim().StartsWith("Routing Transit Number")); fromHere = innerText.Skip(st); foreach (var val in fromHere) { if (RegexCatalog.IsRegexMatch(val.Trim(), RoutingTransitNumber.REGEX_PATTERN, out rt)) { break; } } return(new List <dynamic> { new { RoutingNumber = rt, Rssd = rssd, BankName = nm, FdicCert = fdicCert } }); }