Regular expression Automate Validation Regular expression Validation need

  • Slides: 10
Download presentation
Regular expression

Regular expression

Automate Validation – Regular expression • Validation need a hard and very complex programming.

Automate Validation – Regular expression • Validation need a hard and very complex programming. • Sometimes it looks easy but actually it is not. • So there is a lot of time and efforts waste in this process. • What is the solution? • Regular expression (regex or regexp for short) is a special text string for describing a pattern. • Pattern is an element repeat in a predictable manner.

Cont. • Examples – All telephone numbers have the same form. – All real

Cont. • Examples – All telephone numbers have the same form. – All real numbers have the same form. – All the names have the same form – So on. • Two steps to use regular expression: 1. Learn how to write pattern in RE. 2. Passing RE to Library which give you the ability to use it in validity by automata theory.

Simple Patterns • • d or [0 -9] A digit D or [^0 -9]

Simple Patterns • • d or [0 -9] A digit D or [^0 -9] A non-digit s or [ tnx 0 Bfr] A whitespace character S or [^s] A non-whitespace character w or [a-z. A-Z_0 -9] A word character W or [^w] A non-word character. Any character Note: everyone of those pattern represent only one thing one digit one character and so on.

Using Regular Expression in validation • Fortunately java support Regular expression and give us

Using Regular Expression in validation • Fortunately java support Regular expression and give us a good library which compile the pattern and make you able to using in validation. • regex = "\d"; • string 1="3"; • System. out. println( string 1+string 1. matches(regex));

Complex pattern As we say simple pattern only one thing Mostly we need more

Complex pattern As we say simple pattern only one thing Mostly we need more than one thing Ex: number of two digits This called complex which contain one or more simple with reputation • Then we can write our example as dd • What about five digits? One hundred digits • This is illogical to repeat d one hundred time we need new concept. • •

Repetition in Regx • • X? once or not at all X* zero or

Repetition in Regx • • X? once or not at all X* zero or more times X+ one or more times X{n} exactly n times X{n, } at least n times X{n, m} at least n but not more than m times Where x is pattern Now we can write the previous example by d{100}

Grouping • Regex has two type of grouping – Group mean or (use []

Grouping • Regex has two type of grouping – Group mean or (use [] to represent) – Group mean and (use () to represent) • Examples – [abc] mean one character of set {a , b , c} – (abc) mean three characters follow each other – (d[. ])mean digit follow with dot like 1. and 2.

Ranging in Regex • [a-z] one of small characters from a to z •

Ranging in Regex • [a-z] one of small characters from a to z • [A-Z] one of capital characters from A to Z • [a-z. A-Z] one of small characters from a to z or one of capital characters from A to Z • [a-ck-z] one of small characters from a to c or one of small characters form k to z (mean one of small characters out characters group between c to k. • [^abc] Any character except a, b, or c • [a-z&&[def]] d, e, or f (intersection)

Cont. • [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction) •

Cont. • [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction) • [a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)