Perl does not contain regular expression matching of a string

Source: Internet
Author: User

I. Several Questions
1. Find a string that does not contain "ABC"
Test Case 1 ==> ABC (non-conforming)
Test Case 2 ==> xabc (not met)
Test Case 3 ==> abcy (not met)
Test Case 4 ==> xabcy (not met)
Test Case 5 ==> XXXX xabcy (not met)
Test Case 6 ==> XXX abcy dabc (not met)
Test Case 7 ==> abcy (not met)
Test Case 8 ==> XYZ (compliant)

2. Extract the specified position (Column 2nd) string, which does not contain "ABC"
Test Case 1 ==> 111 ABC 222 (not met)
Test Case 2 ==> 111 xabc 222 (not met)
Test Case 3 ==> 111 abcy 222 (not met)
Test Case 4 ==> 111 xabcy 222 (not met)
Test Case 5 ==> 111 XYZ 222 (compliant)



Ii. Zero-width assertion
The above problems can all be solved using zero-width assertions. First, we need to understand the zero-width assertions:
1.(? = Exp ):This zero-width assertion is used to assert that it can match the expression EXP after the position where it appears. Consider the following regular expression Q (? = U). This regular expression indicates matching the Q of U.
2.(?! Exp ):This zero-width assertion is used to assert that it cannot match the expression EXP after it appears. Let's look at the following regular expression Q (?! U), this regular expression indicates that the matching character is not u's Q
3.(? <= Exp ):This zero-width assertion is used to assert that the expression exp can be matched before the position where it appears.
4.(? <! Exp ):This zero-width assertion is used to assert that the expression exp cannot be matched before the position where it appears.

 

3. Procedures
1. Find a string that does not contain "ABC"
Step 1: ^ (.) + $ (string composition)
Step 2: ^ ((?! ABC).) + $ (the string after the cursor position before each character cannot be ABC)

Use strict; </P> <p> my @ lines = ('abc', 'xabc', 'abcy ', 'xabcy', 'xxxx xabcy ', 'xxx abcy dabc', 'abcy ', 'xyz'); <br/> my $ pattern =' ^ ((?! ABC).) + $ '; </P> <p> foreach my $ line (@ lines) {<br/> if ($ line = ~ /$ Pattern/) {<br/> Print "$ line: matched, $1/N"; <br/>}else {<br/> Print "$ line: mismatched/N "; <br/>}</P> <p>

 

Result:

ABC: mismatched
Xabc: mismatched
Abcy: mismatched
Xabcy: mismatched
XXXX xabcy: mismatched
Xxx abcy dabc: mismatched
Abcy: mismatched
XYZ: matched, Z

2. Extract the specified position (Column 2nd) string, which does not contain "ABC"
Step 1:/S + (/s) +)/S + (first, the second column)
Step 2:/S + (((?! ABC)/s) +)/S + (the cursor position before each character in the second column cannot be a string after ABC)

 

My @ lines = ('2017 ABC 111 ', '2017 xabc 222', '2017 abcy 111 ', '2017 xabcy 222', '2017 XYZ 111 '); <br/> my $ pattern = '^/S + (((?! ABC)/s) +)/S + $ '; </P> <p> foreach my $ line (@ lines) {<br/> if ($ line = ~ /$ Pattern/) {<br/> Print "$ line: matched, $1, $2, $3/N "; <br/>}else {<br/> Print "$ line: mismatched/N"; <br/>}</P> <p>

D:/temp/perl> Perl test. pl
ABC: mismatched
Xabc: mismatched
Abcy: mismatched
Xabcy: mismatched
XXXX xabcy: mismatched
Xxx abcy dabc: mismatched
Abcy: mismatched
XYZ: matched, Z

 

Reference: Use a zero-width assertion to match a row http://www.khotyn.com/category/programming/page/2/ that does not contain consecutive strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.