Analyzing Strings with sscanf

Analyzing Strings with sscanf

The counterpart or "opposite" of sprintf is sscanf. It looks at a string, and extracts values from it according to another string with format specifiers. It then places the values it has extracted in its arguments. The arguments must be variables, or other things that you can assign values to. For example, after the following code segment, the variable f will contain -3.6, and the variable i will contain 17:

float f;
int i;
string the_string = "foo -3.6 fum dum 17";
sscanf(the_string, "foo %f fum dum %d", f, i);

Note that sscanf isn't an ordinary method or built-in function, but a very special construction in Pike. The reason is that sscanf has to change its arguments, and since Pike uses call-by-value, no ordinary method or built-in function can do that.

The format string consists of characters, that should match the contents of the first string exactly. Blanks are significant. For example, the part fum dum in the format string must match a part of the analyzed string (the_string) exactly. The format string can also contain "percent specifiers", which match various things. For example, %d us used to match an integer.

Here is a list of format specifiers:

SpecifierMeaning

%d

An integer in normal decimal (that is, base-10) notation.

%o

An integer in octal (base-8) notation.

%x

An integer in hexadecimal (base-16) notation.

%D

An integer in decimal, or (if it starts with 0) octal, or (if it starts with 0x) hexadecimal notation. Hence, sscanf("12", "%D", i), sscanf("014", "%D", i) and sscanf("0xC", "%D", i) all yield the value 12 in i.

%f

A floating-point number.

%c

The character code of a single character.

%s

A string. If %s is followed by %d, %s will read any non-numerical characters. If followed by %[], %s will read any characters not present in the set in the %[]. If followed by normal text, %s will match all characters up to, but not including, the first occurrence of that text.

%Ns

As above, but a string of exactly N characters

%[characters]

A string containing any of the characters in the list characters.

A minus sign can be used to give a range of values, so e. g. %[a-d] means a string consisting of any of the characters a, b and c.

A ^ sign means "not", so e. g. %[^abc] means any character except a, b and c.

They can be combined, so %[a-cf] means a, b, c, and f.

%{format%}

Repeatedly matches the format specifier format as many times as possible, and gives an array of arrays with the results. Example: %{%d%} matches zero or more integers.

%%

A single percent (%) character

If an asterisk (*) is put between the percent and the operator, e. g. %*d, the operator will only match its argument, and not assign any variables.

sscanf returns the number of percent specifiers that were successfully matched.

The matching done by sscanf is rather simple-minded. It looks at the format string up to the next %, and tries to match that with the analyzed string. If successful, it then goes on to the next part of the format string. If a part of the format string does not match, sscanf immediately returns (with the number of percent specifiers that were successfully matched). Variables in the argument list that correspond to percent specifiers that were not successfully matched will not be changed.

Some examples of sscanf:

  • This call to sscanf will return 1, and the variable a will be given the value "oo":

    sscanf("foo", "f%s", a);

  • The return value from sscanf will be 2. a will be given the value 4711. b will be given the value "bar".

    sscanf("4711bar", "%d%s", a, b);

  • The return value from sscanf will be 1, a will be given the value "test":

    sscanf(" t test", "%*[ t]%s", a)

  • This removes "the " from the beginning of the string in str. If str does not begin with "the ", it will not be changed

    sscanf(str, "the %s", str);

  • This assigns "foo" to s1 and "fum" to s2, and the array ({ ({ 1 }), ({ 2 }), ({ 3 }) }) to a. The return value will be 3.

    sscanf("foo % 1 2 3 fum",
           "%s %% %{%d%} %s", s1, a, s2);

A feature with sscanf is that you can define variables inside it:

sscanf("foo % 1 2 3 fum",
       "%s %% %{%d%} %s",
       string s1, array(string) a, string s2);

In contrast to variables defined in a for statement, these variables are available in the rest of the block, and not just inside the sscanf.

http://docs.roxen.com/pike/7.0/tutorial/strings/sscanf.xml

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24790158/viewspace-1042509/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24790158/viewspace-1042509/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值