1

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

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

从我想要的地方

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

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

4

2 回答 2

1

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

function get_bracketed_words($str) 
{
    if (preg_match_all('/<[a-z]+>/', $str, $matches)) {
        return join('', $matches[0]);
    }
    return '';
}
于 2012-11-12T09:34:10.103 回答
0

如果你使用这个,它应该不会太慢(这里以 Perl 代码为例):

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