"Draft version, read carefully"
Reference Document: Regular Expression 30-minute introductory tutorial
For systematic learning of regular expression content, please visit the tutorial above.
The regular expression matches a character or string according to the specified rule. ‘.‘ ' \b ' \d ' et cetera is called a meta-character in a regular expression, such as a dot character used to match any character other than a newline, \b metacharacters are used to match words, \s metacharacters are used to match spaces, and so on.
Regular expression conversions:
Show You the Code: (\s)
1 stringinput ="I am Lily.";2 stringPattern =@"\s+";3 stringReplacement =" ";4 stringresult =Regex.Replace (input, pattern, replacement);5 6Console.WriteLine ("Original String: {0}", input);7Console.WriteLine ("Replacement String: {0}", result);
\s is used to match spaces, while \s+ is used to match one or more and a space, as shown in the code, to replace one or more spaces with a space, running the effect
Show You the Code: (\b)
1 stringinput ="I am Lily.";2 stringPattern =@"\bis\b";3 stringReplacement ="am";4 stringresult =Regex.Replace (input, pattern, replacement);5Console.WriteLine ("Original String: {0}", input);6Console.WriteLine ("Replacement String: {0}", result);
\b is used to match words, the \bis\b is used to match the word is, and is replaced with AM, running the effect
Show You the code: (. *)
input ="Sis a Girld"; Pattern=@"Is\b.*\bgirl"; Replacement="*"; Result=Regex.Replace (input, pattern, replacement); Console.WriteLine ("Original String: {0}", input); Console.WriteLine ("Replacement String: {0}", result);
. * Used to match any number of non-newline characters, the pattern match is preceded by the IS Girl end, and is behind and girl must precede a space string, run effect
Verify that the regular expression matches:
Show You the Code: (\d)
1 stringPhone ="";2 stringPattern =@"1\d{10}";3 BOOLresult =false;4Console.WriteLine ("Please enter your mobile phone number.");5 Do6 {7Phone =console.readline ();8result =Regex.IsMatch (phone, pattern);9 if(!result)TenConsole.WriteLine ("Badnum,try again"); One} while(!result); AConsole.WriteLine ("Good");
\d is used to match a single digit, the d{10} is used to match 10 digits and runs as follows:
Continuous update ...
How regular expressions are used in C #