Programming C# 2nd Edition phần 5

Làm sạch bất kỳ tài nguyên nào đang được sử dụng khu vực thiết kế biểu mẫu Windows tạo ra mã phương pháp bắt buộc để hỗ trợ thiết kế - không thay đổi nội dung của phương pháp này với các trình biên tập mã. Thêm bất kỳ mã constructor | Programming C 2nd Edition string stringl 04 03 27 Jesse Liberty regular expression which groups company twice Regex theReg new Regex @ time d s @ company S s @ ip d . s @ company S s get the collection of matches MatchCollection theMatches string1 iterate through the collection foreach Match theMatch in theMatches if 0 theMatch 0 time 0 time ip 0 ip Company 0 company iterate over the captures collection in the company group within the groups collection in the match foreach Capture cap in company .Captures cap 0 Output theMatch 04 03 27 Jesse Liberty time 04 03 27 ip Company Liberty cap Jesse cap Liberty The code in bold iterates through the Captures collection for the Company group. foreach Capture cap in company .Captures Let s review how this line is parsed. The compiler begins by finding the collection that it will iterate over. theMatch is an object that has a collection named Groups. The Groups collection has an indexer that takes a string and returns a single Group object. Thus the following line returns a single Group object 231 Programming C 2nd Edition company The Group object has a collection named Captures. Thus the following line returns a Captures collection for the Group stored at Groups company within the theMatch object company .Captures The foreach loop iterates over the Captures collection extracting each element in turn and assigning it to the local variable cap which is of type Capture. You can see from the output that there are two capture elements Jesse and Liberty. The second one overwrites the first in the group and so the displayed value is just Liberty. However by examining the Captures collection you can find both values that were captured. 232 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
2    123    2    21-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.