Perl Regex,获取两个字符串之间的字符串 [英] Perl Regex, get strings between two strings
问题描述
我是 Perl 的新手,并尝试使用 Regex 在两个标签之间获取一段字符串,我知道该字符串中将存在该字符串.我已经尝试过来自 stackoverflow 的各种答案,但似乎没有一个对我有用.这是我的例子...
I am new to Perl and trying to use Regex to get a piece of string between two tags that I know will be there in that string. I already tried various answers from stackoverflow but none of them seems to be working for me. Here's my example...
所需的数据在 $info 变量中,我想从中获取有用的数据
The required data is in $info variable out of which I want to get the useful data
my $info = "random text i do not want\n|BIRTH PLACE=Boston, MA\n|more unwanted random text";
上述字符串中的有用数据是 Boston, MA
.我通过 $info =~ s/\n//g;
从字符串中删除了换行符.现在$info
有这个字符串我不想要的随机文本|出生地=波士顿,马萨诸塞|更多不需要的随机文本"
.我认为这样做可以帮助我轻松捕获所需的数据.
The Useful Data in the above string is Boston, MA
. I removed the newlines from the string by $info =~ s/\n//g;
. Now $info
has this string "random text i do not want|BIRTH PLACE=Boston, MA|more unwanted random text"
. I thought doing this will help me capture the required data easily.
请帮助我获取所需的数据.我确信数据总是以 |BIRTH PLACE=
开头并以 |
结尾.之前和之后的所有内容都是不需要的文本.如果已经回答了这样的问题,也请指导我.谢谢.
Please help me in getting the required data. I am sure that the data will always be preceded by |BIRTH PLACE=
and succeeded by |
. Everything before and after that is unwanted text. If a question like this is already answered please guide me to it as well. Thanks.
推荐答案
$info =~ m{\|BIRTH PLACE=(.*?)\|} or die "There is no data in \$info?!";
my $birth_place = $1;
这应该可以解决问题.
这篇关于Perl Regex,获取两个字符串之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!