3.2.5.2 類比C函數 scanf()功能

來源:互聯網
上載者:User

標籤:python   milang   Regex   

在Python裡,沒有與scanf()直接等同的功能函數,因此需要格式化輸入,就需要使用Regex的功能來實現,並且Regex的功能比scanf()更加靈活,功能更加強大,下面就來列出一些等同的表達:

scanf()格式字串

Regex

%c

.

%5c

.{5}

%d

[-+]?\d+

%e,%E,%f,%g

[-+]?(\d+(\.d*)?|\.\d+)([eE][-+]?\d+)?

%i

[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)

%o

[-+]?[0-7]+

%s

\S+

%u

\d+

%x,%X

[-+]?(0[xX])?[\dA-Fa-f]+

 

 

輸入一個字串的例子:

/usr/sbin/sendmail - 0 errors, 4 warnings

對於上面格式的字串,如果使用C函數scanf()來輸入,需要使用下面的格式來實現:

%s - %d errors, %d warnings

如果我們使用Regex來表示,如下:

(\S+) - (\d+) errors, (\d+) warnings

例子:

print(‘scanf()‘)

pattern = re.compile(r"(\S+) - (\d+) errors, (\d+) warnings")

match = pattern.match(‘/usr/sbin/sendmail - 0 errors, 4 warnings‘)

if match:

    print(match.groups())

結果輸出如下:

scanf()

(‘/usr/sbin/sendmail‘, ‘0‘, ‘4‘)

 

%c的例子:

print(‘scanf() %c‘)

pattern = re.compile(r".")

match = pattern.match(‘this is for test\n‘)

if match:

    print(match.group())

結果輸出如下:

scanf() %c

t

 

%5c的例子:

print(‘scanf() %5c‘)

pattern = re.compile(r".{5}")

match = pattern.match(‘this is for test\n‘)

if match:

    print(match.group())

結果輸出如下:

scanf() %5c

this 

 

%e, %E, %f, %g的例子:

print(‘scanf() %e, %E, %f, %g‘)

pattern = re.compile(r"[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?")

match = pattern.match(‘+200.3721\n‘)

if match:

    print(match.group())

match = pattern.match(‘x9876\n‘)

if match:

    print(match.group())#不匹配沒有輸出

結果輸出如下:

scanf() %e, %E, %f, %g

+200.3721

 

%i的例子:

print(‘scanf() %i‘)

pattern = re.compile(r"[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)")

match = pattern.match(‘0xAA55\n‘)

if match:

    print(match.group())

match = pattern.match(‘234.56\n‘)

if match:

    print(match.group())

結果輸出如下:

scanf() %i

0xAA55

234

 

八進位的%o的例子:

print(‘scanf() %o‘)

pattern = re.compile(r"[-+]?[0-7]+")

match = pattern.match(‘0756\n‘)

if match:

    print(match.group())

match = pattern.match(‘898\n‘)

if match:

    print(match.group())#不匹配沒有輸出

結果輸出如下:

scanf() %o

0756

 

字串%s的例子:

print(‘scanf() %s‘)

pattern = re.compile(r"\S+")

match = pattern.match(‘深圳是一個小漁村\n‘)

if match:

    print(match.group())

match = pattern.match(‘898\n‘)

if match:

    print(match.group())

結果輸出如下:

scanf() %s

深圳是一個小漁村

898

 

%u的例子:

print(‘scanf() %u‘)

pattern = re.compile(r"\d+")

match = pattern.match(‘756\n‘)

if match:

    print(match.group())

match = pattern.match(‘-898\n‘)

if match:

    print(match.group())#不匹配沒有輸出

結果輸出如下:

scanf() %u

756

 

十六進位%x, %X的例子:

print(‘scanf() %x %X‘)

pattern = re.compile(r"[-+]?(0[xX])[\dA-Fa-f]+")

match = pattern.match(‘0x756\n‘)

if match:

    print(match.group())

match = pattern.match(‘-898\n‘)

if match:

    print(match.group())#不匹配沒有輸出

結果輸出如下:

scanf() %x %X

0x756




蔡軍生 QQ:9073204  深圳

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

3.2.5.2 類比C函數 scanf()功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.