Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expression ASCII Converting. Regular Expression Regular Expression is a tool to check if a string matches some rules. It is a very complicated.

Similar presentations


Presentation on theme: "Regular Expression ASCII Converting. Regular Expression Regular Expression is a tool to check if a string matches some rules. It is a very complicated."— Presentation transcript:

1 Regular Expression ASCII Converting

2 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.

3 Regular Expression Definitions \dMatch a digital number or as [0-9] \wMatch any letter of a-z or A-Z or 0-9 and _ \sMatch a white space [abc]Match any one of a, b, c (abc)Match abc exact once (abc)+Match abc one or more times (abc)?Match abc zero or one times (abc)*Match abc zero or more times [^abc]Match any character except of a, b, c \W\S\DNot \w not \s and not \d [ ] means any one, ( ) means all of them $ #

4 More Definitions \bBoundary of a word \blet\b letter \.For period. (dot) \nNewline character.Any character except \n \\Match character slash \ ^Match the beginning of string (l in letter) \zMatch the end of string (y in many) $Match the end of string or end of a line |Or operator (g|s)et (get or set) {n}Exact n times {n, }At least n timed {n, m}At least n times and at most m times

5 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}

6 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}

7 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

8 Example Regex r = new Regex("\d[a-z]"); Match m = r.Match("123gabc456"); if (m.Success) { Response.Write("Found match at position " + m.Index); }

9 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 @

10 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;

11 ASCII Table (0-63)

12 ASCII Table (64-127)

13 String and byte array Converting We can convert string to an byte array and vise versa. The converting object is ASCIIEncoding asciiConvertor = new ASCIIEncoding(); The name space is System.Text We need to use two major functions: byte [] GetBytes(string msg); and string GetString( byte[] data, int offset, int Length);

14 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.


Download ppt "Regular Expression ASCII Converting. Regular Expression Regular Expression is a tool to check if a string matches some rules. It is a very complicated."

Similar presentations


Ads by Google