Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are sscanf functions and regular expressions

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What this article shares with you is about what the sscanf function and regular expression are, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

All the experiments in this article are based on the following procedure:

Char str [10]

For (int I = 0; I < 10; iTunes +) str [I] ='!'

The value of str after execution is

Str = "!"

We initialize each character of str to an exclamation point, and when the value of str changes, we use printf to print the value of str and compare it with the previous exclamation point, so we can easily see how str has changed.

Lab 1:

Sscanf (

"123456"

's'

Str)

;-the value of str is "123456 Universe!"

The experiment is simple: copy the source string "123456" to the first six characters of str, and set the seventh character of str to the null character, which is / 0

Lab 2:

Sscanf (

"123456"

"3s"

Str)

;-the value of str is "123apper0!"

See, the regular expression has an extra 3 after the percent sign, which tells sscanf to copy only three characters to str, and then set the fourth character to the null character.

Lab 3:

Sscanf (

"aaaAAA"

"% [aMusz]"

Str)

;-the value of str is "aaa/0!"

From the beginning of this experiment, we will use regular expressions. Amurz in parentheses is a regular expression that can represent any character from a to z.

In

Before we continue, let's take a look at what the percent sign means, and% means choice.

,% is followed by a condition, such as "% s" in experiment 1, s is a condition that represents any character, and "% s" means only

What you want to type is a character, copy it to str. There is another condition for "% 3s" in experiment 2: only 3 characters are copied. The condition of "% [amurz]" in experiment 3 is a little stricter, and the input

Something is not only a character, but also a lowercase character, so experiment 3 only copied the lowercase letter "aaa" to str, don't forget to add the null character.

Lab 4:

Sscanf (

"AAAaaaBBB"

"% [^ aMusz]"

Str)

;-the value of str is "AAA/0!"

Yes

All characters, as long as they are not lowercase letters, satisfy the "^ a murz" regular expression, and the symbol ^ indicates logical non. The first three characters are not lowercase, so copy them to str, but the last three characters are not

It's lowercase letters. Why not copy it to str?

Lab 5:

Sscanf (

"AAAaaaBBB"

"% [Amurz]% [amurz]"

Str)

;-paragraph error

this

The original intention of the experiment is to copy uppercase letters to str and then lowercase letters to str, but unfortunately, a segment error occurs when the program is running, because when sscanf scans the character a, it violates

Contrary to the condition "% [Amurz]", sscanf stops execution and no longer scans the subsequent characters, so the second condition does not make any sense. This experiment shows that the% sign cannot be used twice or more.

Lab 6:

Sscanf (

"AAAaaaBBB"

"% * [Amurz]% [aMuz]"

Str)

;-the value of str is "aaa/0!"

this

Characters that meet the criteria, in this experiment,% * [Amurz] filters all uppercase letters, and then uses% [amurz] to put them.

Copy the lowercase letters after to str. If there is only% * and no%, sscanf will not copy any characters to str, and the purpose of sscanf is simply to filter strings.

Lab 7:

Sscanf (

"AAAaaaBBB"

"% [aMusz]"

Str)

;-the value of str is "!"

After the first few experiments, we all know that after the sscanf copy is complete, a null character will be added after the str, but if none of the characters meet the criteria, sscanf will not add the null character after the str, and the value of str is still 10 exclamation points. This experiment also shows that if you don't use% * to filter out the characters you don't need before, you'll never get the middle characters.

Lab 8:

Sscanf (

"AAAaaaBC="

"% * [Amurz]% * [aMuz]% [^ aMuz =]"

Str)

;-the value of str is "BC/0!"

This is a comprehensive experiment, but the purpose of this experiment is not to help us review what we have learned before, but to show two noteworthy points:

Note that 1% can only be used once, but% * can be used many times. For example, in this experiment, first filter uppercase letters with% * [Amurz], and then filter lowercase letters with% * [a murz].

Note 2: ^ can be followed by multiple conditions, and these conditions are affected by ^, for example, ^ a murz = represents ^ a M z and ^ = (neither a lowercase letter nor an equal sign).

Lab 9:

Int k

Sscanf (

"AAA123BBB456"

"% * [^ 0-9]% I"

& k)

The value of-k is 123

First,% * [^ 0-9] filters the previous non-numeric characters, then converts the numeric characters into int integers with% I and copies them to the variable k. Note that the parameter must use the address of k.

This is what sscanf functions and regular expressions are, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report