【发布时间】:2016-11-10 05:53:14
【问题描述】:
我正在尝试通过将正则表达式模式应用于整个字符串来获取所需的单词。
这是我的字符串:
Birla Sun Life Global Real Estate Fund - Retail Plan - Direct Plan - Growth Option
SBI GOLD FUND - DIRECT PLAN - DIVIDEND
我想得到Plan 类型,即Direct
Plan 有时会出现一次,有时会出现两次,因此该模式必须适用于两种情况。
这是我目前所写的:
$pname = 'Birla Sun Life Global Real Estate Fund - Retail Plan - Direct Plan - Growth Option';
if ( $pname =~ / ([^\s]*) plan(?!^plan$)*/ig ) # regex to get plan type
{
$plan_type = $1;
}
print "$1";
但它给出的输出是 Retail 而不是 Direct。
我应该怎么做才能获得Direct 作为Plan 类型?
【问题讨论】:
-
“Plan type which is Direct”是什么意思?您需要字符串中的哪些特定短语或单词?
-
您是否要在最后一次出现
Plan之前获取单词? -
你试过regex101.com吗?
标签: regex perl regex-negation