1. Trang chủ
  2. » Mẫu Slide

Session5 module6 7 generic regularexpression

33 639 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 33
Dung lượng 769,39 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

m mới bắt đầu học java nên chưa có kinh nghiệm gì, nên mong các tiền bối đi trước cho em vài lời khuyên. trước đây em có học qua về c++ với C và php nhưng không thực sự giỏi cái nào. hiện nay em đang có bài tập lớn về java,mà em thì không có kinh nghiệm gì về nó nên muốn chọn mảng nào dễ hơn để có thể dễ bắt đầu, nếu các anh chị nào đã học qua thì cho em 1 vài lời khuyên với.em xin cám ơn các anh chị trước.

Trang 1

JPII, Session 5

Module 6 – Generic

Module 7 – Regular Expression

Trang 2

Java Collection Review

duplicate Its learnt implementations are ArrayList and

LinkedList

are HashSet, LinkedSet and LinkHashSet.

structrure.

key and its value If we have a key, we can get its value Keys

can not duplicate, but values can.

One of them is sort.

Advanced Java / Session3 / 2 of 33

2

A Guide To Advanced Java – Module 6

Trang 3

Module 6:Objectives

Advanced Java / Session3 / 3 of 33

A Guide To Advanced Java – Module 6

Trang 4

“I’d like a Cup of T”

Advanced Java / Session3 / 4 of 33

A Guide To Advanced Java – Module 6

Trang 5

• Genericity is a way by which programmers can specify the type of object

that a class can work with via parameters passed at declaration time and

evaluated at compile time

compiler so that it can be checked

Advanced Java / Session5 / 5 of 29

5

A Guide To Advanced Java – Module 6

Trang 6

Advanced Java / Session1 / 6 of 13

List list = new ArrayList();

Integer numberInt = new Integer(9);

list.add(numberInt);

Integer n=(Integer) list.get(0);

System.out.println(list);

 Before generics, you would write this piece of code:

 With generics, this code becomes:

List<Number> list = new ArrayList<Number>();

Number numberInt = new Integer(9); list.add(numberInt);

Integer n= list.get(0);

System.out.println(list);

Trang 7

Advantages and Limitations of Generics

• Generics allow flexibility of

dynamic binding

to check for type correctness

of the program at the compile

time.

• In generics the compiler detected

to fix than runtime errors

• The code reviews are simpler in

generics as the ambiguity is

less between containers.

• In Generic codes contains lesser

casts and thus helps to improve

readability and robustness

In generics you cannot create

generic constructors.

• A local variable cannot be declared where the key and value types are different from each other.

Advanced Java / Session3 / 7 of 33

A Guide To Advanced Java – Module 6

Trang 8

• Syntax to create an instance of the generic class

ClassName<specific datatype> obj = new ClassName<specific

datatype>();

Advanced Java / Session3 / 8 of 33

A Guide To Advanced Java – Module 6

Trang 9

Generic method

Advanced Java / Session3 / 9 of 33

A Guide To Advanced Java – Module 6

Trang 10

Using Wildcards with Generics

There are three types of wildcards used with generics.

Advanced Java / Session3 / 10 of 33

A Guide To Advanced Java – Module 6

Trang 11

Exception Handling with Generics

Advanced Java / Session5 / 11 of 29

11

A Guide To Advanced Java – Module 6

Click to edit Master text styles

Second level

Third level

Fourth level

Fifth level

Advanced Java / Session3 / 11 of 33

A Guide To Advanced Java – Module 6

Trang 12

Inheritance with Generics

Click to edit Master text styles

Second level

Third level

Fourth level

Fifth level

Advanced Java / Session3 / 12 of 33

A Guide To Advanced Java – Module 6

Trang 13

Using Legacy Code in Generics

• In Java, genericity ensures that the same class file is generated by both legacy and generic version with some additional information about types

generic type like collection is used without a type parameter, it is called a raw type

• Erasure removes all generic type information All the type information

between angle brackets is thrown out

Advanced Java / Session3 / 13 of 33

A Guide To Advanced Java – Module 6

Trang 14

Using Generics in Legacy Code

• A generic library should be created when there is access to source code

• Update the entire library source as well as the client code to eliminate

potential unchecked warning

Advanced Java / Session5 / 14 of 29

14

A Guide To Advanced Java – Module 6

Trang 15

Module Review

Advanced Java / Session5 / 15 of 29

15

A Guide To Advanced Java – Module 6

Trang 18

What is RegEx?

RegEx is a set of symbols and syntactic elements which are

used to match patterns in a text.

Trang 19

Regular Expression API

• The three main classes present in the regex API

– java.util.regex.Pattern : the Pattern object is a compiled regular expression that can be applied to any number

of strings

– java.util.regex.Matcher : the Matcher object is an individual instance of that regex being applied to a specific

target string

– java.util.regex.PatternSyntaxException: object is an unchecked exception that indicates a syntax error in

a regular expression pattern

Trang 20

Regular Expression Pattern Syntax

Trang 21

String Literals

• Literal is a constant value used to initialize variables

• Metacharacters are the special characters which effect the way a pattern is matched They are ([{\^-$|}])?*+

character:

– Precede the metacharacter with a backslash

– Enclose the metacharacter by specifying \Q at the beginning and \E at the end.

Advanced Java / Session5 / 21 of 33

21

A Guide To Advanced Java – Module 7

Trang 22

Character classes

Character Classes

[abc] a, b, or c (simple class)

[^abc] Any character except a, b, or c (negation)

[a-zA-Z] a through z, or A through Z, inclusive (range)

[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)

[a-z&&[def]] d, e, or f (intersection)

[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)

Advanced Java / Session5 / 22 of 33

22

A Guide To Advanced Java – Module 7

Trang 23

Predefined Character Classes

Predefined Character Classes

Any character (may or may not match line terminators)

Trang 24

Boundary Matchers

 Boundary Matchers

 ^  The beginning of a line

 $  The end of a line

 \b  A word boundary

 \B  A non-word boundary

 \A  The beginning of the input

 \G  The end of the previous match

 \Z  The end of the input but for the final terminator, if any

 \z  The end of the input

Advanced Java / Session5 / 24 of 33

24

A Guide To Advanced Java – Module 7

Trang 25

• Quantifiers allow the user to specify the number of occurrences for a

match to match against

• There are three types of quantifiers:

Trang 26

 Quantifiers

 Meaning

 Greedy  Reluctant  Possessive

 X?  X??  X?+  X, once or not at all

 X*  X*?  X*+  X, zero or more times

 X+  X+?  X++  X, one or more times

 X{n}  X{n}?  X{n}+  X, exactly n times

 X{n,}  X{n,}?  X{n,}+  X, at least n times

 X{n,m}  X{n,m}?  X{n,m}+  X, at least n but not more than m times

Advanced Java / Session5 / 26 of 33

26

A Guide To Advanced Java – Module 7

Trang 27

Demo: Differences Among Greedy, Reluctant, and Possessive

Quantifiers

Enter your regex: *foo // greedy quantifier

Enter input string to search: xfooxxxxxxfoo

I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13

Enter your regex: *?foo // reluctant quantifier

Enter input string to search: xfooxxxxxxfoo

I found the text "xfoo" starting at index 0 and ending at index 4.

I found the text "xxxxxxfoo" starting at index 4 and ending at index 13

Enter your regex: *+foo // possessive quantifier

Enter input string to search: xfooxxxxxxfoo

No match found.

Enter your regex: *foo // greedy quantifier

Enter input string to search: xfooxxxxxxfoo

I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13

Enter your regex: *?foo // reluctant quantifier

Enter input string to search: xfooxxxxxxfoo

I found the text "xfoo" starting at index 0 and ending at index 4.

I found the text "xxxxxxfoo" starting at index 4 and ending at index 13

Enter your regex: *+foo // possessive quantifier

Enter input string to search: xfooxxxxxxfoo

Trang 28

Capturing Groups

• In capturing group, multiple characters are treated as a single unit, which

are placed inside single parentheses

to right

• Consider an expression ((1)(2(3))) The expression has four capture

groups as listed below :

– (1)

– (3)

Here, the groupCount() method returns a value of 4 which tells that

there are 4 capturing groups.

Advanced Java / Session5 / 28 of 33

28

A Guide To Advanced Java – Module 7

Trang 29

Back references

• Backreferencing is a process in which a particular section of the string

matching the captured group is kept in memory for future use

• To recall a particular group, you may use backslash followed by a digit

indicating the number of group This can be specified as a backreference in the regular expression

– String regex: (\w\w)\1

– String input: papa

– Result: Found “pa” in group(1)

Advanced Java / Session5 / 29 of 33

A Guide To Advanced Java – Module 7 29

Trang 30

Module Review

– Use character classes and techniques for regular expressions.

– Use boundary matchers.

– Use quantifiers.

Advanced Java / Session5 / 30 of 33 A Guide To Advanced Java – Module 7 30

Trang 32

Next …

framework (Module 8,9) and explore the Annotation and

Trang 33

Advanced Java / Session5 / 33 of 29

A Guide To Advanced Java – Module 7 33

Ngày đăng: 22/11/2014, 16:39

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w