【发布时间】:2013-07-08 19:18:03
【问题描述】:
我想从文件中解析一些信息。
文件中的信息:
Rita_bike_house_Sha9
Rita_bike_house
我想要像dis这样的输出
$a = Rita_bike_house and $b = Sha9,
$a = Rita_bike_house and $b = "original"
为了得到,我使用了以下代码:
$name = @_; # This @_ has all the information from the file that I have shown above.
#For matching pattern Rita_bike_house_Sha9
($a, $b) = $name =~ /\w\d+/;
if ($a ne "" and $b ne "" ) { return ($a,$b) }
# this statement doesnot work at all as its first condition
# before the end is not satisified.
有什么方法可以将“Rita_bike_house”存储在$a 和“Sha9”存储在$b?我认为我的正则表达式缺少某些东西。你有什么建议吗?
【问题讨论】:
-
$name = @_是代码异味。你的意思可能是($name)=@_。 -
\w也匹配_(下划线),所以你需要更精确的匹配规则。 -
很抱歉。是的,它是 ($name) = @_;
-
我厌倦了模式匹配为 /\w_\w_\w_([\w\d]+)/。但是,它不起作用。模式匹配中有什么建议吗?谢谢
-
\w只匹配一个字母。您需要\w+来匹配一个或多个字母(一个单词)。但是\w也与_匹配,所以如果这就是你的意思,你可能最好使用[a-z]。
标签: perl