如何用正则只取字符串里的一小段,保存为另一个字符串?



QUOTE:
原帖由 ynchnluiti 于 2009-1-8 15:59 发表

$str =~ />(.+)  

><间的字符用(.+)就可以了吗
+是匹配多个的意思,点呢,点是任意一个字符的意思吗

楼主这个问题可以。
呵呵,勤学好学
.   Match any character
+      Match 1 or more times
Using Simple "Wildcards" and "Repetitions"
Calling these "wildcards" may actually conflict with the theoretical grammer and syntax of Perl, but in fact is the most intuitive way to think of it, and will not lead to any coding mistakes.

.   Match any character
\w  Match "word" character (alphanumeric plus "_")
\W  Match non-word character
\s  Match whitespace character
\S  Match non-whitespace character
\d  Match digit character
\D  Match non-digit character
\t  Match tab
\n  Match newline
\r  Match return
\f  Match formfeed
\a  Match alarm (bell, beep, etc)
\e  Match escape
\021  Match octal char ( in this case 21 octal)
\xf0  Match hex char ( in this case f0 hexidecimal)

You can follow any character, wildcard, or series of characters and/or wildcard with a repetiton. Here's where you start getting some power:

*      Match 0 or more times
+      Match 1 or more times
?      Match 1 or 0 times
{n}    Match exactly n times
{n,}   Match at least n times
{n,m}  Match at least n but not more than m times

Now for some examples:

$string =~ m/\s*rem/i;   #true if the first printable text is rem or REM

$string =~ m/^\S{1,8}\.\S{0,3}/;   # check for DOS 8.3 filename
                                   #  (note a few illegals can sneak thru)


QUOTE:
原帖由 huanghaojie 于 2009-1-8 16:55 发表
呵呵,勤学好学
.   Match any character
+      Match 1 or more times

多学多练


QUOTE:
原帖由 huanghaojie 于 2009-1-8 16:58 发表
Using Simple "Wildcards" and "Repetitions"
Calling these "wildcards" may actually conflict with the theoretical grammer and syntax of Perl, but in fact is the most intuitive way to think of it,  ...

\s   这个到底怎么解释 ,什么叫whitespace    就是指 [ \t\r\n] and space,吗


QUOTE:
原帖由 huanghaojie 于 2009-1-8 21:21 发表

\s   这个到底怎么解释 ,什么叫whitespace    就是指 [ \t\r\n] and space,吗

[ \t\r\n]里面第一个是空格

还有\f

[Copy to clipboard] [ - ]
CODE:
[ \t\r\n\f]