正则表达式~!

erbao78 2005-07-12 10:50:30
"/1 President/2 of/3 Siemens/4 (/5 China/6 )/7 Limited/8 Company/9 ,/10 Kehenen/11 made/12 the/13 above/14 comment/15 in/16 a/17 recent/18 interview/19 by/20 reporter/21 ./22
现在知道后面的数字,想取出前面的单词,用正则表达式怎样写?(比如,知道数字9,想取前面对应的单词Company)
比如$ number = 9;
$English_word = $English_sentence =~/(.*?)\/$number\s+/g;
我写的正则表达式不对,请教一些各位~!
...全文
159 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AllCHN 2005-08-04
  • 打赏
  • 举报
回复
($English_word) = ($English_sentence =~/([^\s]*)\/$number\D?/);
print $English_word;

在正则表达式的 $English_Word 一定要括起来,否则返回是否找到的标志,可能是 1 或者 0,用括号括起来才能返回匹配元串的单词
AllCHN 2005-08-04
  • 打赏
  • 举报
回复
($English_word) = ($English_sentence =~/([^\s]*)\/$number\D?/);
print $English_word
hanhai118 2005-08-01
  • 打赏
  • 举报
回复
他说的很对
xyzxyz1111 2005-07-12
  • 打赏
  • 举报
回复
$_ = "/1 President/2 of/3 Siemens/4 (/5 China/6 )/7 Limited/8 Company/9 ,/10 Kehenen/11 made/12 the/13 above/14 comment/15 in/16 a/17 recent/18 interview/19 by/20 reporter/21 ./22";

$number = 9;

print $1 if(/(\S+)\/$number/);

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧