1、介绍perl
先看例子
if ($input =~ m/^([-+]?[0-9]+)([CF])$/)
{
# If we get in here, we had a match. $1 is the number, $2 is "C" or "F".
$InputNum = $1; # save to named variables to make the . . .
$type = $2; # . . . rest of the program easier to read.
}
$input表示变量input,=~表示将匹配结果链接到变量$input中,m/.../表示里面的是正则表达式,$1、$2在perl中表示获取括号中的内容。
即$variable =~ m/regex/。
2、匹配浮点型温度的正则表达式
形如9C、2.31f、8.37 c等。规则为浮点数+c或f(可不加,大小写均可)
浮点数:^[-+]?[0-9]+(/.[0-9]*)?$
浮点数+c或f:^([-+]?[0-9]+(/.[0-9]*)?)([CF])$
浮点数+空格或tab+c或f:^([-+]?[0-9]+(/.[0-9]*)?)/s*([CF])$
3、Non-Capturing Parentheses:(?:...)
注意(?:...)是一个整体,表示()内的仅被group而不被capture,这是(?:...)与()的唯一区别。
如^([-+]?[0-9]+(?:/.[0-9]*)?)([CF])$,这里$2不再表示(?:/.[0-9]*)?),而表示([CF])
先看例子
if ($input =~ m/^([-+]?[0-9]+)([CF])$/)
{
# If we get in here, we had a match. $1 is the number, $2 is "C" or "F".
$InputNum = $1; # save to named variables to make the . . .
$type = $2; # . . . rest of the program easier to read.
}
$input表示变量input,=~表示将匹配结果链接到变量$input中,m/.../表示里面的是正则表达式,$1、$2在perl中表示获取括号中的内容。
即$variable =~ m/regex/。
2、匹配浮点型温度的正则表达式
形如9C、2.31f、8.37 c等。规则为浮点数+c或f(可不加,大小写均可)
浮点数:^[-+]?[0-9]+(/.[0-9]*)?$
浮点数+c或f:^([-+]?[0-9]+(/.[0-9]*)?)([CF])$
浮点数+空格或tab+c或f:^([-+]?[0-9]+(/.[0-9]*)?)/s*([CF])$
3、Non-Capturing Parentheses:(?:...)
注意(?:...)是一个整体,表示()内的仅被group而不被capture,这是(?:...)与()的唯一区别。
如^([-+]?[0-9]+(?:/.[0-9]*)?)([CF])$,这里$2不再表示(?:/.[0-9]*)?),而表示([CF])