regexes for parsing formatted numbers

后端 未结 3 1533
后悔当初
后悔当初 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:31

    As I understand it, each line comprises one or more fixed-width fields, which may contain labels, spaces, or data of different kinds. If you know the widths and types of the fields, extracting their data is a simple matter of substring(), trim() and (optionally) Whatever.parseWhatever(). Regexes can't make that job any easier--in fact, all they can do is make it a lot harder.

    Scanner doesn't really help, either. True, it has predefined regexes for various value types, and it does the conversions for you, but it still needs to be told which type to look for each time, and it needs the fields to be separated by a delimiter it can recognize. Fixed-width data doesn't require delimiters, by definition. You might be able to fake the delimiters by doing a lookahead for however many characters should be left in the line, but that's just another way of making the job harder than it needs to be.

    It sounds like performance is going to be a major concern; even if you could make a regex solution work, it would probably be too slow. Not because regexes are inherently slow, but because of the contortions you would have to go through to make them fit the problem. I suggest you forget about regexes for this job.

    0 讨论(0)
  • 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()
    } 
    
    0 讨论(0)
  • 2021-01-22 13:40

    It is only a partial answer but I was alerted to Scanner in Java 1.5 which can scan text and interpret numbers which gives a BNF for the numbers that can be scanned and interpreted by this Java utility. In principle I imagine the BNF could be used to construct a regex.

    0 讨论(0)
提交回复
热议问题