0

I know how to format a string using %f, %d etc. using util.format(). Can someone tell me which is the complementary function that enables SCANNING from a string (not from console input).

For example:

Running...

const util = require('util');
var weatherStr = util.format(`The temperature at %d o' clock was %f deg. C and the humidity was %f.`, 5, 23.9, 0.5);
console.log(weatherStr);

...generates...

The temperature at 5 o' clock was 23.9 deg. C and the humidity was 0.5.

I was expecting a util function which would work such that running the following code...

const util = require('util');
var weatherStr = 'The temperature at 5 o' clock was 23.9 deg. C and the humidity was 0.5.';
console.log(util.????(tempStr, `humidity was %f.`));

...generates...

0.5

Which is the util function that does this? I don't think "parseFloat" will work because it will extract 23.9.

I'm new to JS and Node but I expected a "scan" function. I know there is a scanf npm library but it seems to work with console input rather than existing strings. I have been doing searches for "%f" among JS and Node functions and surprisingly util.format seems to be the only one with a mention of it.

2 Answers 2

1

I don't know of any scan library like that, but you can use regular expressions. Here are some patterns you could use:

  • Integer: [+-]?\d+
  • Decimal: [+-]?\d+(?:\.\d+)?

If you put these in a capture group, you can access the corresponding matches from the array that String#match returns:

var weatherStr = "The temperature at 5 o'clock was 23.9 deg. C and the humidity was 0.5.";
console.log(+weatherStr.match(/humidity was ([+-]?\d+(?:\.\d+)?)./)[1]);

You could create a utility function that can deal with %d and %f:

function scanf(input, find) {
    var pattern = {
        "d": "(\\d+)",
        "f": "(\\d+(?:\\.\\d+)?)"
    };
    find = find
        // Escape characters for use in RegExp constructor:
        .replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
        // Replace %-patterns
        .replace(/((?:\\)*)%([a-z])/g, function (m, a, b) {
            return a.length % 4 == 0 && b in pattern ? a + pattern[b] : m;
        });
    var match = input.match(new RegExp(find));
    return match && match.slice(1).map(Number);
}

var weatherStr = "The temperature at 5 o'clock was 23.9 deg. C and the humidity was 0.5.";
console.log(scanf(weatherStr, "humidity was %f"));
console.log(scanf(weatherStr, "at %d o'clock was %f"));

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks trincot!

Actually it turns out the scanf npm library (https://www.npmjs.com/package/scanf) DOES solve my problem. I just hadn't read it all the way through. I had to install "sscanf" (note the double-s) as well. The sscanf method (listed at the bottom of the package page) works just as I expected.

I'm surprised this package is not more popular, but it is what I need. Thanks again!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.