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