繁体   English   中英

正则表达式提取字符串的一部分

[英]regular expression to extract a part of string

我有以下来自核心银行系统的交易格式

This is a <test>  and only <test> hope <u> understand

从我想要的地方

<test><test><u> (along with <>)

用简单的子字符串我可以做到这一点,但是它会太慢..有没有办法使用正则表达式功能在< and >之间捕获文本?

我能想到的最简单的方法是使用preg_match_all()然后join()的结果一起形成最终的字符串:

function get_bracketed_words($str) 
{
    if (preg_match_all('/<[a-z]+>/', $str, $matches)) {
        return join('', $matches[0]);
    }
    return '';
}

如果使用此功能,它应该不会太慢(此处以Perl代码为例):

while (my $line = <FILE>) {
    my ($request) = ($line =~ /RequestArray:(.*)/);
    next unless $request;
    # here, you can split $requests to sub-pieces using another regex
    # ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM