- 论坛徽章:
- 0
|
正规表达式中<[^>]+>为什么就能匹配所有的<*> ??
我的理解...[]中以^开头的表示匹配所有非>的字符
整个<[^>]+>表示匹配以<开头后面跟着至少一个的非>字符,然后再接着>
用来匹配html的标签....我也搞不懂为什么<[^>]+>就能匹配所有的
html标签....
那至少也应该是匹配"非<>"
一段perl代码:
- $/ = ".\n"; 1 # a special "chunk-mode"'; chunks end with a period-newline
- combination
- while (<>) 2
- {
- next unless s 3
- {# (regex starts here)
- ### Need to match one word:
- \b # start of word....
- ( [a-z]+ ) # grab word, filling $1(and \1).
- ### Now need to allow any number of spaces and/or <TAGS>
- ( # save what intervenes to $2.
- ( # ($3-parens onlyforgrouping the alternation)
- \s # whitespace (includes newline, which is good).
- | # -or-
- <[^>]+> # item like < TAG>.
- )+ # need at least one of the above, but allow more.
- )
- ### Now match the first word again:
- (\l\b) # \b ensures not embedded. This copy saved to $4.
- # (regex ends here)
- }
- # Above is the regex. Replacement string, below, followed by the modifiers, /i, /g,
- and /x
- "\e[7m$1\e[m$2\e[7m$4\e[m"igx; 4
- s/^ ([^\e]*\n)+//mg; 5 # Remove any unmarked lines.
- s/^/$ARGV: /mg; 6 # Ensure lines begin with filename.
- print;
- }
复制代码 |
|