140 likes | 340 Views
Regular Expression ASCII Converting. Regular Expression. Regular Expression is a tool to check if a string matches some rules. It is a very complicated topic. We only give some basic rules here. Suppose that ~ means match. Then expression d means a digital number. So
E N D
Regular Expression Regular Expression is a tool to check if a string matches some rules. It is a very complicated topic. We only give some basic rules here. Suppose that ~ means match. Then expression \d means a digital number. So "2" ~ \d "77023" not ~ \d If we want to match more digital numbers, we have "77023" ~ \d+ If we want to match exact 5 digital numbers, have "77023" ~ \d{5} Here {5}means repeating 5 times. expression \w means any letter of a-z or A-Z or 0-9 and_ expression \s means white space.
Regular Expression Definitions [ ] means any one, ( ) means all of them $ #
Regular Expression (RE) examples The zip code three formations: 77023 77023-4578 77023 4578 So we first has expression \d{5} for 77023 Then we have either – or white space or none So it is [\s-]? The last 4 digits is optional, so is (\d{4})? or ([0-9]{4})? Thus we got \d{5}([\s-]\d{4})? The email address like tand@uhv.edu Its RE is \w+@(\w+\.)+\w{2,3}
The telephone number could be: 713-567-3491 or (713)567-3491 (713) 567-3491 RE of 713-567-3491 is \d{3}-\d{3}-\d{4} RE of (713)567-3491 and (713) 567-3491 \(\d{3}\)\s?\d{3}-\d{4} So combine the together, we have (\(\d{3}\)\s?|\d{3}-)\d{3}-\d{4}
Class Regex and Match • Using System.Text.RegularExpressions; • Class Regex object is used to search regular expression in the string. • The constructor is Regex(string RE) the argument RE is the regular expression • Basic operator is Match Match(string str) the argument str is the string to be searched the return value is Match object • Match ::Success Success ==true means found match • Match :: Index is the position of the match
Example Regex r = new Regex("\d[a-z]"); Match m = r.Match("123gabc456"); if (m.Success) { Response.Write("Found match at position " + m.Index); }
Replace operators for string string str = "Can you lend me your car?"; string str2 = str.Replace("you", "he"); Then str2 = "Can he lend me her car?"; However, if use regular expression replace string str3 = Regex.Replace(str, @"you\b", "he"); Then str3 = "Can he lend me your car?"; The regular expression string must prefix @
Char and byte converting • Every byte is 8 bits, so its range is from 0 to 255. • If the first bit is 0, the byte is used to express characters. Some character is not written- able, such as space. • Convert a single Character is easy Char ch = 'A'; int n = (int)ch; Char c = (Char)99;
String and byte array Converting We can convert string to an byte array and vise versa. The converting object is ASCIIEncoding asciiConvertor = newASCIIEncoding(); The name space is System.Text We need to use two major functions: byte [] GetBytes(string msg); and string GetString( byte[]data, intoffset, int Length);
Data Structure object There are three major data structure objects (I) ArrayList the major operation is Add(…) and Remove() (ii) Stack the major operation is Push(…) and Pop() (iii) Queue the major operation is Enque(…) and Deque() Their name space is System.Collections. Note: They must add class objects. Any element read from those structure class object need cast to get the oringinal class data type.