Regular Expression ASCII Converting. Regular Expression Regular Expression is a tool to check if a...

14
Regular Expression ASCII Converting

Transcript of Regular Expression ASCII Converting. Regular Expression Regular Expression is a tool to check if a...

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

Regular ExpressionASCII Converting

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

Regular ExpressionRegular 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.

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

Regular Expression Definitions\d Match a digital number or as [0-9]

\w Match any letter of a-z or A-Z or 0-9 and _

\s Match 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\D Not \w not \s and not \d

[ ] means any one, ( ) means all of them $ #

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

More Definitions\b Boundary of a word \blet\b letter

\. For period . (dot)

\n Newline character

. Any character except \n

\\ Match character slash \

^ Match the beginning of string (l in letter)

\z Match 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

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

Regular Expression (RE) examplesThe 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 [email protected]

Its RE is \w+@(\w+\.)+\w{2,3}

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

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}

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

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 searchedthe return value is Match object

• Match ::Success

Success ==true means found match• Match :: Index is the position of the match

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

Example Regex r = new Regex("\d[a-z]");

Match m = r.Match("123gabc456"); if (m.Success) { Response.Write("Found match at position "

+ m.Index); }

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

Replace operators for string

string str = "Can you lend me your car?";

string str2 = str.Replace("you", "he");

Thenstr2 = "Can he lend me her car?";

However, if use regular expression replacestring str3 = Regex.Replace(str, @"you\b", "he");

Thenstr3 = "Can he lend me your car?";

The regular expression string must prefix @

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

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;

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

ASCII Symbol ASCII Symbol ASCII Symbol ASCII Symbol0 NUL 16 DLE 32 (space) 48 01 SOH 17 DC1 33 ! 49 12 STX 18 DC2 34 " 50 23 ETX 19 DC3 35 # 51 34 EOT 20 DC4 36 $ 52 45 ENQ 21 NAK 37 % 53 56 ACK 22 SYN 38 & 54 67 BEL 23 ETB 39 ' 55 78 BS 24 CAN 40 ( 56 89 TAB 25 EM 41 ) 57 9

10 LF 26 SUB 42 * 58 :11 VT 27 ESC 43 + 59 ;12 FF 28 FS 44 , 60 <13 CR 29 GS 45 - 61 =14 SO 30 RS 46 . 62 >15 SI 31 US 47 / 63 ?

ASCII Table (0-63)

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

ASCII Table (64-127)

ASCII Symbol ASCII Symbol ASCII Symbol ASCII Symbol64 @ 80 P 96 ` 112 p65 A 81 Q 97 a 113 q66 B 82 R 98 b 114 r67 C 83 S 99 c 115 s68 D 84 T 100 d 116 t69 E 85 U 101 e 117 u70 F 86 V 102 f 118 v71 G 87 W 103 g 119 w72 H 88 X 104 h 120 x73 I 89 Y 105 i 121 y74 J 90 Z 106 j 122 z75 K 91 [ 107 k 123 {76 L 92 \ 108 l 124 |77 M 93 ] 109 m 125 }78 N 94 ^ 110 n 126 ~79 O 95 _ 111 o 127 �

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

String and byte array ConvertingWe 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);

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

Data Structure objectThere are three major data structure objects

(I) ArrayListthe major operation is Add(…) and Remove()(ii) Stackthe major operation is Push(…) and Pop()(iii) Queuethe 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.