[英]How do I extract last element between angle brackets using regular expressions?
How do I extract last element between angle brackets using regular expressions? 如何使用正则表达式提取尖括号之间的最后一个元素? The last element is the currency, I only need GBP
or USD
. 最后一个元素是货币,我只需要GBP
或USD
。
<ABZ>-<BOS>-<Mon Dec 28 11:20:00 UTC 2015>-<Mon Dec 28 18:05:00 UTC 2015>-<EI>-<3241>-<EI>-<139>-<USBR>-<ROUND>-<GBP>
<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<NONE>-<ONEWAY>-<GBP>
<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<NONE>-<ROUND>-<USD>
<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<NSNS>-<ROUND>-<GBP>
<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<USBC>-<ONEWAY>-<GBP>
<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<USBC>-<ROUND>-<GBP>
<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<USBC>-<ROUND>-<USD>
I have tried /&\\<[AZ]+\\>(?=$)/
and /\\<[A-Za-z]+\\>$/
but it's not working. 我已经尝试过/&\\<[AZ]+\\>(?=$)/
和/\\<[A-Za-z]+\\>$/
但无法正常工作。
<[^>]*>\s*$
Try this.See demo. 试试看。看演示。
https://regex101.com/r/vP2zF2/4 https://regex101.com/r/vP2zF2/4
$re = "/<[^>]*>\\s*$/m";
$str = "<ABZ>-<BOS>-<Mon Dec 28 11:20:00 UTC 2015>-<Mon Dec 28 18:05:00 UTC 2015>-<EI>-<3241>-<EI>-<139>-<USBR>-<ROUND>-<GBP>\n<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<NONE>-<ONEWAY>-<GBP>\n<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<NONE>-<ROUND>-<USD>\n<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<NSNS>-<ROUND>-<GBP>\n<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<USBC>-<ONEWAY>-<GBP>\n<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<USBC>-<ROUND>-<GBP>\n<ABZ>-<BOS>-<Mon Jan 04 11:20:00 UTC 2016>-<Mon Jan 04 18:05:00 UTC 2016>-<EI>-<3241>-<EI>-<139>-<USBC>-<ROUND>-<USD>";
preg_match_all($re, $str, $matches);
I have used my own expression (without escaped characters) and used it like this: 我使用了自己的表达式(没有转义字符),并使用了它,如下所示:
$str = "<ABZ>-<BOS>-<Mon Dec 28 11:20:00 UTC 2015>-<Mon Dec 28 18:05:00 UTC 2015>-<EI>-<3241>-<EI>-<139>-<USBR>-<ROUND>-<GBP>";
preg_match("/<[A-Za-z]+>$/", $str, $match);
$match = str_replace(array('<', '>'), array('', ''), $match[0]);
$currency = $match;
echo $currency;
I didn't need /gm
as it's not multiline, it's a CSV extracted line by line. 我不需要/gm
因为它不是多行的,而是逐行提取的CSV文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.