regexes for parsing formatted numbers

后端 未结 3 1537
后悔当初
后悔当初 2021-01-22 13:00

I am parsing documents which contain large amounts of formatted numbers, an example being:

 Frc consts  --     1.4362                 1.4362                 5.41         


        
3条回答
  •  感情败类
    2021-01-22 13:40

    You could start with this and go from there.

    This regex matches all the numbers you've provided.
    Unfortunatly, it also matches the 3 in 3 1.23-

    // [-+]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][-+]?[0-9]+)?
    // 
    // Match a single character present in the list “-+” «[-+]?»
    //    Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
    // Match the regular expression below «(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)»
    //    Match either the regular expression below (attempting the next alternative only if this one fails) «[0-9]+(?:\.[0-9]*)?»
    //       Match a single character in the range between “0” and “9” «[0-9]+»
    //          Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    //       Match the regular expression below «(?:\.[0-9]*)?»
    //          Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
    //          Match the character “.” literally «\.»
    //          Match a single character in the range between “0” and “9” «[0-9]*»
    //             Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    //    Or match regular expression number 2 below (the entire group fails if this one fails to match) «\.[0-9]+»
    //       Match the character “.” literally «\.»
    //       Match a single character in the range between “0” and “9” «[0-9]+»
    //          Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    // Match the regular expression below «(?:[eE][-+]?[0-9]+)?»
    //    Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
    //    Match a single character present in the list “eE” «[eE]»
    //    Match a single character present in the list “-+” «[-+]?»
    //       Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
    //    Match a single character in the range between “0” and “9” «[0-9]+»
    //       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    Pattern regex = Pattern.compile("[-+]?(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+)(?:[eE][-+]?[0-9]+)?");
    Matcher matcher = regex.matcher(document);
    while (matcher.find()) {
        // matched text: matcher.group()
        // match start: matcher.start()
        // match end: matcher.end()
    } 
    

提交回复
热议问题