简体   繁体   中英

regular expression to extract two items from a long string

There are some strings having the following type of format,

{abc=1234457, cde=3,  label=3352-4e9a-9022-1067ca63} <chve>  abc?  123.456.789, http=appl.com 

I would like to extract 1234457 and 3352-4e9a-9022-1067ca63 , which correspond to abc and label respectively.

This is the javascript I have been trying to use, but it does not work. I think the regular expression part is wrong.

var headerPattern = new RegExp("\{abc=([\d]*),,label=(.*)(.*)");
if (headerPattern.test(row)) {
   abc = headerPattern.exec(row)[0];
    label = headerPattern.exec(row)[1];
}

Try: abc=(\\d*).*?label=([^}]*)

Explanation

  • abc= literal match
  • (\\d*) catch some numbers
  • .*? Lazy match
  • label= literal match
  • ([^}]*) catch all the things that aren't the closing brace

Here is what I came up with:

\{abc=(\d+).*label=(.+)\}.*

Your have two problems in \\{abc=([\\d]*),,label=(.*)(.*) :

  • Using abc=([\\d]*),, , you are looking for abc=([\\d]*) followed by the literal ,, . You should use .* instead. Since .* is nongreedy be default, it will not match past the label .
  • By using label=(.*)(.*) , the first .* captures all the remaining text. You want to only catch text until the edge of the braces, so use (.*)}.* .

Disclaimer: Made with a Java-based regex tester. If anything in JavaScript regexes would invalidate this, feel free to comment.

You can do it the following way:

var row = '{abc=1234457, cde=3,  label=3352-4e9a-9022-1067ca63} <chve>  abc?  123.456.789, http=appl.com';

var headerPatternResult = /{abc=([0-9]+),.*?label=([a-z0-9\-]+)}/.exec(row);

if (headerPatternResult !== null) {
    var abc = headerPatternResult[1];
    var label = headerPatternResult[2];

    console.log('abc: ' + abc);
    console.log('label: ' + label);
}

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