public static string RegExp(string data, string strMatch, bool fullValue = false) { string ret = ""; System.Text.RegularExpressions.Regex oRegex = new System.Text.RegularExpressions.Regex(strMatch); System.Text.RegularExpressions.Match oMath = oRegex.Match(data); if (fullValue) { return(oMath.Value); } while (oMath.Success) { for (int i = 1; i <= 2; i++) { System.Text.RegularExpressions.Group g = oMath.Groups[i]; if (g.ToString() != "") { ret = g.ToString(); return(ret); } } oMath = oMath.NextMatch(); } return(ret); }
protected override void ProcessRecord() { if (InputObject == null) { return; } PSObject newInputObject; if (NoClone.IsPresent) { newInputObject = Helpers.EnsureHasProperties(InputObject, Property); } else { newInputObject = Helpers.CloneObject(InputObject, Property); } bool setValue = true; PSVariable matchVar = new PSVariable("Matches"); if (whereIsScriptBlock) { var varList = new List <PSVariable>(); varList.Add(new PSVariable("_", InputObject)); varList.Add(matchVar); var whereResult = whereAsScriptBlock.InvokeWithContext(null, varList, null); setValue = LanguagePrimitives.IsTrue(whereResult); } else if (whereIsString) { setValue = false; var whereProperty = newInputObject.Properties[whereAsString]; if (Match != null && whereProperty != null && whereProperty.Value != null) { var matchResult = matchRegex.Match(whereProperty.Value.ToString()); setValue = matchResult.Success; Hashtable matches = new Hashtable(StringComparer.CurrentCultureIgnoreCase); foreach (string groupName in matchGroupNames) { System.Text.RegularExpressions.Group g = matchResult.Groups[groupName]; if (g.Success) { int keyInt; if (Int32.TryParse(groupName, out keyInt)) { matches.Add(keyInt, g.ToString()); } else { matches.Add(groupName, g.ToString()); } } } matchVar.Value = matches; } else { setValue = whereProperty != null && LanguagePrimitives.IsTrue(whereProperty.Value); } } if (!setValue) { WriteObject(newInputObject); return; } object newValue = Value; if (valueIsScriptBlock) { var varList = new List <PSVariable>(); varList.Add(new PSVariable("_", InputObject)); varList.Add(matchVar); var scriptResult = valueAsScriptBlock.InvokeWithContext(null, varList, null); if (scriptResult.Count == 1) { newValue = scriptResult[0]; } else { newValue = scriptResult; } } foreach (string property in Property) { if (!IfUnset.IsPresent || Helpers.IsPropertyNullOrWhiteSpace(newInputObject, property)) { if (JoinWith != null) { var enumValue = LanguagePrimitives.ConvertTo <string[]>(newValue); newValue = string.Join(JoinWith, enumValue); } newInputObject.Properties.Add(new PSNoteProperty(property, newValue)); } } WriteObject(newInputObject); }