/// <summary> /// 修复xml格式。 /// 属性加上双引号,"/>"前面加上空白 /// </summary> /// <param name="poorXml"></param> /// <returns></returns> public static string XmlRepair(string poorXml) { if (string.IsNullOrEmpty(poorXml)) { return(poorXml); } var okend = RegexElementStop.Replace(poorXml, @" />"); var xml = RegexElementHead.Replace(okend, mb => { return(RegexAttribute.Replace(mb.Value, ma => { var k = ma.Groups["k"].Value; var v = ma.Groups["v"].Value; if (v.Length >= 2) { var h = v.First(); var t = v.Last(); if ((h == '\'' && t == '\'') || (h == '‘' && t == '’') || (h == '’' && t == '’') || (h == '“' && t == '”') || (h == '”' && t == '”')) { v = v.Substring(1, v.Length - 2); } } return k + "=\"" + v + "\""; })); }); return(xml); }
public void RegexTest() { RegexAttribute attribute = new RegexAttribute(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); regexNode.Email = "*****@*****.**"; List<ValidationError> errors = new List<ValidationError>(); attribute.Validate(regexNode, emailInfo, errors, ServiceProvider); Assert.AreEqual(0, errors.Count); }
public void RegexViolationTest() { RegexAttribute attribute = new RegexAttribute(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); regexNode.Email = "joeblow"; ValidationErrorCollection errors = new ValidationErrorCollection(); attribute.Validate(regexNode, emailInfo, errors); Assert.AreEqual(1, errors.Count); }
public void RegexTest() { RegexAttribute attribute = new RegexAttribute(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); regexNode.Email = "*****@*****.**"; List <ValidationError> errors = new List <ValidationError>(); attribute.Validate(regexNode, emailInfo, errors, ServiceProvider); Assert.AreEqual(0, errors.Count); }
/// <summary> /// Draws a text entry field which allows text that doesn't match the attribute's pattern. /// </summary> private void DrawDisplayInvalidField(Rect canvas, SerializedProperty property, GUIContent label, RegexAttribute attribute) { Color backgroundResetColor = GUI.backgroundColor; Color displayColor = backgroundResetColor; string displayText = property.stringValue; // check to see if we have a record of an invalid entry for this property bool inputMatchesPattern = this._invalidEntriesCache.ContainsKey(property.propertyPath) == false; // if we do, switch the display to lookin' like invalid mode if (inputMatchesPattern == false) { displayColor = Color.red; // and change the display text to whatever that bad value is displayText = this._invalidEntriesCache[property.propertyPath]; label = new GUIContent(label); label.tooltip = string.Format("This field failed to match the regular expression '{0}'. It's value will not be saved when you navigate away from this inspector!", attribute.Pattern); } GUI.backgroundColor = displayColor; EditorGUI.BeginChangeCheck(); string newValue = EditorGUI.TextField(canvas, label, displayText); if (EditorGUI.EndChangeCheck()) { if (Regex.IsMatch(newValue, attribute.Pattern, attribute.MatchOptions) == false) { this._invalidEntriesCache.AddOrSet(property.propertyPath, newValue); } else { this._invalidEntriesCache.Remove(property.propertyPath); property.stringValue = newValue; property.serializedObject.ApplyModifiedProperties(); } } GUI.backgroundColor = backgroundResetColor; }
public RegexAttributeTest() { _validator = new RegexAttribute("^[a]*$"); }
public void SetupAttribute() { Attr = new RegexAttribute(RegexPattern); }
/// <summary> /// Draws a text entry field where text is immediately discarded if it doesn't match the attribute's pattern. /// </summary> private void DrawForcedField(Rect canvas, SerializedProperty property, GUIContent label, RegexAttribute attribute) { EditorGUI.BeginChangeCheck(); string newValue = EditorGUI.TextField(canvas, label, property.stringValue); if (EditorGUI.EndChangeCheck()) { if (Regex.IsMatch(newValue, attribute.Pattern, attribute.MatchOptions) == false) { return; } property.stringValue = newValue; property.serializedObject.ApplyModifiedProperties(); } }