简体   繁体   中英

regular expression to extract a part of string

I have following format of transaction from core banking system

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

from where i want

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

with simple substring i can do that , but it will be too slow .. is there any way to capture a text between < and > using regex functions?

The easiest I can think of is to use preg_match_all() and then join() the results together to form the final string:

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

If you use this, it should not be too slow (Perl code as an example here):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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