Regex









How do I use Regex
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PhotonConverter.cs Copy
323     static string PregReplace(string input, string[] pattern, string[] replacements)
324     {
325         if (replacements.Length != pattern.Length)
326             Debug.LogError("Replacement and Pattern Arrays must be balanced");
327
328         for (var i = 0; i < pattern.Length; i++)
329         {
330             input = Regex.Replace(input, pattern[i], replacements[i]);
331         }
332
333         return input;
334     }
File name: PhotonConverter.cs Copy
335     static string PregReplace(string input, string pattern, string replacement)
336     {
337         return Regex.Replace(input, pattern, replacement);
338
339     }
File name: ImportTiled2Unity.Material.cs Copy
19         public Material FixMaterialForMeshRenderer(string objName, Renderer renderer)
20         {
21             string xmlPath = ImportUtils.GetXmlPath(objName);
22
23             XDocument xml = XDocument.Load(xmlPath);
24
25             // The mesh to match
26             string meshName = renderer.name;
27
28             // The mesh name may be decorated by Unity
29             string pattern = @"_MeshPart[\d]$";
30             Regex regex = new Regex(pattern);
31             meshName = regex.Replace(meshName, "");
32
33             var assignMaterials = xml.Root.Elements("AssignMaterial");
34
35             // Find an assignment that matches the mesh renderer
36             XElement match = assignMaterials.FirstOrDefault(el => el.Attribute("mesh").Value == meshName);
37
38             if (match == null)
39             {
40                 // The names of our meshes in the AssignMaterials elements may be wrong
41                 // This happened before when Unity replaced whitespace with underscore in our named meshes
42                 // That case is handled now, but there may be others
43                 StringBuilder builder = new StringBuilder();
44                 builder.AppendFormat("Could not find mesh named '{0}' for material matching\n", renderer.name);
45                 string choices = String.Join("\n ", assignMaterials.Select(m => m.Attribute("mesh").Value).ToArray());
46                 builder.AppendFormat("Choices are:\n {0}", choices);
47
48                 Debug.LogError(builder.ToString());
49                 return null;
50             }
51
52             string materialName = match.Attribute("material").Value;
53             string materialPath = ImportUtils.GetMaterialPath(materialName);
54
55             // Assign the material
56             renderer.sharedMaterial = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
57
58             // Set the sorting layer for the mesh
59             string sortingLayer = match.Attribute("sortingLayerName").Value;
60             if (!String.IsNullOrEmpty(sortingLayer) && !SortingLayerExposedEditor.GetSortingLayerNames().Contains(sortingLayer))
61             {
62                 Debug.LogError(string.Format("Sorting Layer \"{0}\" does not exist. Check your Project Settings -> Tags and Layers", sortingLayer));
63                 renderer.sortingLayerName = "Default";
64             }
65             else
66             {
67                 renderer.sortingLayerName = sortingLayer;
68             }
69
70             // Set the sorting order
71             renderer.sortingOrder = ImportUtils.GetAttributeAsInt(match, "sortingOrder");
72
73             // Do we have an alpha color key?
74             string htmlColor = ImportUtils.GetAttributeAsString(match, "alphaColorKey", "");
75             if (!String.IsNullOrEmpty(htmlColor))
76             {
77                 // Take for granted color is in the form '#RRGGBB'
78                 byte r = byte.Parse(htmlColor.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);
79                 byte g = byte.Parse(htmlColor.Substring(3, 2), System.Globalization.NumberStyles.HexNumber);
80                 byte b = byte.Parse(htmlColor.Substring(5, 2), System.Globalization.NumberStyles.HexNumber);
81                 Color color = new Color32(r, g, b, 255);
82                 renderer.sharedMaterial.SetColor("_AlphaColorKey", color);
83             }
84
85             return renderer.sharedMaterial;
86         }
File name: usableFunction.cs Copy
50         private static Regex Email_Address()
51         {
52             string Email_Pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
53                 + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?
54                 + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
55
56             return new Regex(Email_Pattern, RegexOptions.IgnoreCase);
57         }
File name: usableFunction.cs Copy
59         private static Regex StringOnly()
60         {
61             string StringAndNumber_Pattern = "^[a-zA-Z]";
62
63             return new Regex(StringAndNumber_Pattern, RegexOptions.IgnoreCase);
64         }
File name: usableFunction.cs Copy
66         private static Regex NumbersOnly()
67         {
68             string StringAndNumber_Pattern = "^[0-9]*$";
69
70             return new Regex(StringAndNumber_Pattern, RegexOptions.IgnoreCase);
71         }
File name: usableFunction.cs Copy
73         private static Regex ValidPassword()
74         {
75             string Password_Pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,15})$";
76
77             return new Regex(Password_Pattern, RegexOptions.IgnoreCase);
78         }
File name: frmCustomerRegistration.cs Copy
168         private void Email_Address_Validating(object sender, CancelEventArgs e)
169         {
170             System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
171             if (txtEmail_Address.Text.Length > 0)
172             {
173                 if (!rEMail.IsMatch(txtEmail_Address.Text))
174                 {
175                     MessageBox.Show("invalid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
176                     txtEmail_Address.SelectAll();
177                     e.Cancel = true;
178                 }
179             }
180         }
File name: frmCustomerRegistration.cs Copy
187         private void Username_Validating(object sender, CancelEventArgs e)
188         {
189             System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9_]");
190             if (txtUsername.Text.Length > 0)
191             {
192                 if (!rEMail.IsMatch(txtUsername.Text))
193                 {
194                     MessageBox.Show("only letters,numbers and underscore is allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
195                     txtUsername.SelectAll();
196                     e.Cancel = true;
197                 }
198             }
199         }
File name: frmRegistration.cs Copy
193         private void Email_Address_Validating(object sender, CancelEventArgs e)
194         {
195             System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
196             if (txtEmail_Address.Text.Length > 0)
197             {
198                 if (!rEMail.IsMatch(txtEmail_Address.Text))
199                 {
200                     MessageBox.Show("invalid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
201                     txtEmail_Address.SelectAll();
202                     e.Cancel = true;
203                 }
204             }
205         }

Download file with original file name:Regex

Regex 171 lượt xem

Gõ tìm kiếm nhanh...