You are on page 1of 16

|

Python
python search string import
2011-04-20 09:22 27809 (1)

http://blog.csdn.NET/whycadi/archive/2008/01/02/2011046.aspx


1 Python

1.1

1.2

1567873 1.2.1

17583
1.3

380 1.4

354 2 re
245
2.1 compile
2
125 2.2 match search

2.3 finditer

2.4

3 re

3.1 Pattern

Continious Integration (23) 3.2 Match


C/C++ (1)
Java (97) 3.2.1
Linux (71)
3.2.2 Match
perl (21)
Python (67)
4
(52)
(9)
(17)
(63)
(8)
Python Python str
soft skills (10)

(7)

// (8)
(13)

(27)
1 Python

Python re, s=123abc456


201609 (1) abc,
201606 (1)
>>> import re
201605 (8)
201604 (5) >>> s='123abc456eabc789'
201512 (13) >>> re.findall(rabc,s)

['abc', 'abc']

findall(rule , target [,flag] )


Linux3264
(52243) compile
pythonselenium
(43377)
Java finalize

(41224)
Python
(34348)
Python r .. raw
(27782)
/ raw / //, /
ftpEntering Extended Passive Mode
(25568)
maven snapshotrelease 4 / //// r
(24983) raw
Python
(24450)
Python Pyc
(22892) re
JavaBufferedReader

(19644)
. * + | ? ^ $ / /


VC (7)
[ ] { }
Java (5)
/d /w /s /
Python (5)

Python (5)

scwindows (5) # ! : - (?# )

STAX XML (5)

gzip: stdin: not in gzip format (4)

(4)
Java finalize (4)
Java finally
(3) 1.1

[ ]
* 10
* AndroidXposed---
[abc123] a b c 1 2 3
* ChromiumPlugin [ ] - [a-zA-Z]
Module
* Android TV-- [z-a]
TV app
[ ] ^ [^a-zA-Z]
* --MySQL
^ [a-z^A-Z] ^

Release Management |
snowy.: release process

|
pythonweb
xuannina: [a-zA-Z]|[0-9] [a-zA-Z0-9]
C++PythonPerjava
|
-:
[ ] [ ] |
2 /| ;
-:
dog|cat dog cat g c
Python: classmethod & staticmethod
(?: ) I have a dog I have a
kingking27:
maven snapshotrelease cat rI have a (?:dog|cat) rI have a dog|cat
: html

Linux-(/) >>> s = I have a dog , I have a cat
lyshw: /boot /
Python >>> re.findall( rI have a (?:dog|cat) , s )
qq_35737640:
#coding=gbknumbers =
input(": ")numb...
input(": ")numb...
['I have a dog', 'I have a cat'] #
Presentation
Soniyaphilip: This is not really to
be predictable, but it write...
>>> re.findall( rI have a dog|cat , s )
AntHelloWorld
:
['I have a dog', 'cat'] # I have a dog cat

/n S /n

>>> s=123 /n456 /n789

>>> findall(r.+,s)

['123', '456', '789']

>>> re.findall(r.+ , s , re.S)

['123/n456/n789']

^ $

^ [ ] [ ]

compile M

/d

/ /d [0-9]

/D

[^0-9] Python

/w

[a-zA-Z0-9]

/W

/w [^a-zA-Z0-9]

/s

[ /t/r/n/f/v] )

/S

[^ /t/r/n/f/v]

/A

^ /A M

/Z

$ /Z M

>>> s= '12 34/n56 78/n90'

>>> re.findall( r'^/d+' , s , re.M ) #

['12', '56', '90']

>>> re.findall( r/A/d+, s , re.M ) #

['12']

>>> re.findall( r'/d+$' , s , re.M ) #


['34', '78', '90']

>>> re.findall( r/d+/Z , s , re.M ) #

['90']

/b

/s

>>> s = 'abc abcde bc bcd'

>>> re.findall( r/bbc/b , s ) # bc

['bc'] bc

>>> re.findall( r/sbc/s , s ) bc

[' bc '] # bc

/B

/b 0

>>> re.findall( r/Bbc/w+ , s ) # bc bc

['bcde'] # abcde bcde bcd

(?:)

(?: )

ab

>>> s=ababab abbabb aabaab

>>> re.findall( r/b(?:ab)+/b , s )

['ababab']

>>> re.findall( r/b(ab)+/b , s )

['ab']

(group)

(?# )

Python (?# )

(?iLmsux)

Python findall compile

compile

i IGNORECASE L LOCAL m MULTILINE s DOTALL u


UNICODE x VERBOSE

(?i)

(?im)

1.2

Python

* 0

+ 1

>>> s = aaa bbb111 cc22cc 33dd

>>> re.findall( r/b[a-z]+/d*/b , s ) # 1

['aaa', 'bbb111']

/b

>>> re.findall( r[a-z]+/d* , s )

['aaa', 'bbb111', 'cc22', 'cc', 'dd'] #

? 0 1

0 1

123 10e3

>>> s = 123 10e3 20e4e4 30ee5

>>> re.findall( r /b/d+[eE]?/d*/b , s )

['123', '10e3']

123 10e3, /b

1.2.1

Python

{m} m

{m,n} m n (n>m)

{3,} 5 { 5} {0,5}

a 3

b: 2 4

c: 5

d: 4

>>> s= 1 22 333 4444 55555 666666

>>> re.findall( r/b/d{3}/b , s ) # a 3

['333']

>>> re.findall( r/b/d{2,4}/b , s ) # b: 2 4

['22', '333', '4444']

>>> re.findall( r/b/d{5,}/b, s ) # c: 5

['55555', '666666']

>>> re.findall( r/b/d{1,4}/b , s ) # 4

['1', '22', '333', '4444']

*? +? ??

* + ? c /* part 1 */ /*
part 2 */

>>> s =r /* part 1 */ code /* part 2 */

>>> re.findall( r//*.*/*/ , s )


[/* part 1 */ code /* part 2 */]

>>> re.findall( r//*.*?/*/ , s ) # * ?

['/* part 1 */', '/* part 2 */']

1.3

Python

(?<=)

(?=)

c /* */ /* */

>>> s=r/* comment 1 */ code /* comment 2 */

>>> re.findall( r(?<=//*).+?(?=/*/) , s )

[' comment 1 ', ' comment 2 ']

>>> s = aaa111aaa , bbb222 , 333ccc

>>> re.findall( r(?<=[a-z]+)/d+(?=[a-z]+)' , s ) #

error: look-behind requires fixed-width pattern

>>> re.findall( r/d+(?=[a-z]+), s )

['111', '333']

group

>>> re.findall (r'[a-z]+(/d+)[a-z]+' , s )

['111']

(?<!...)

(?!...)

>>> re.findall( r/d+(?!/w+) , s )

['222']

/w [a-z]

>>> re.findall( r/d+(?![a-z]+) , s )

['11', '222', '33']

111 222

Python

Python

1.4

Python

()

(/d+)

>>> s = aaa111aaa , bbb222 , 333ccc

>>> re.findall (r'[a-z]+(/d+)[a-z]+' , s )

['111']

findall ()

(?P<name>)

(?P Python <>

num (?P<num>/d+)

(?P=name)

>>> s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,fff777ggg'

>>> re.findall( r'([a-z]+)/d+([a-z]+)' , s ) #

[('aaa', 'aaa'), ('fff', 'ggg')]

>>> re.findall( r '(?P<g1>[a-z]+)/d+(?P=g1)' , s ) #

['aaa']

>>> re.findall( r'[a-z]+(/d+)([a-z]+)' , s ) #

[('111', 'aaa'), ('777', 'ggg')]

/number

>>> re.findall( r([a-z]+)/d+/1 , s )

['aaa']

>>> s='111aaa222aaa111 , 333bbb444bb33'

>>> re.findall( r'(/d+)([a-z]+)(/d+)(/2)(/1)' , s ) #

[('111', 'aaa', '222', 'aaa', '111')]

Python2.4 re

( ? ( id/name )yes-pattern|no-pattern)
id/name yes-pattern no-pattern

usr@mail < usr@mail > <>

>>> s='<usr1@mail1> usr2@maill2'

>>> re.findall( r'(<)?/s*(/w+@/w+)/s*(?(1)>)' , s )

[('<', 'usr1@mail1'), ('', 'usr2@maill2')]

>>> s='<usr1@mail1> usr2@maill2 <usr3@mail3 usr4@mail4> < usr5@mail5 '

<> <> <>

<>

>>> re.findall( r'(<)?/s*(/w+@/w+)/s*(?(1)>)' , s )

[('<', 'usr1@mail1'), ('', 'usr2@maill2'), ('', 'usr3@mail3'), ('', 'usr4@mail4'), ('', 'usr5@mail5')]

findall match search

Python

2 re

re findall findall

re Python

findall

findall(rule , target [,flag] )

compile

2.1 compile

compile( rule [,flag] )

Pattern

Pattern

findall ( rule , target )

re.compile Regular Expression Object Pattern

>>> s='111,222,aaa,bbb,ccc333,444ddd'

>>> rule=r/b/d+/b

>>> compiled_rule=re.compile(rule)

>>> compiled_rule.findall(s)

['111', '222']

compile compile
|

I I G N O R E C A S E

L L O C A L /w
[a-zA-Z0-9] "" ""

M MULTILINE ^( ) $( )

>>> s=123 456/n789 012/n345 678

>>> rc=re.compile(r^/d+) # M

>>> rc.findall(s)

['123'] # 123

>>> rcm=re.compile(r^/d+,re.M) # M

>>> rcm.findall(s)

['123', '789', '345'] #

$ M 678

456 012 678 .

>>> rc=re.compile(r/d+$)

>>> rcm=re.compile(r/d+$,re.M)

>>> rc.findall(s)

['678']

>>> rcm.findall(s)

['456', '012', '678']

S DOTALL . . /n .

/n

U U N I C O D E /w , /W , /b , /B , /d , /D , /s /S Uni
code

X VERBOSE #

>>> rc
= re.compile(
r
"
/d+|[a-zA-Z]+
")

>>> rc
= re.compile(r"""

# start a rule

/d+

#
number

|
[a-zA-Z]+

#
word

""", re.VERBOSE)

'/ '
'/'

2.2 match search

match( rule , targetString [,flag] )

search( rule , targetString [,flag] )

re match search compile Pattern match search Pattern

match search

compile

Match

findall match search

findall

match( rule , targetString [,flag] )

search( rule , targetString [,flag] )

MatchObject .

matchObject

NoneType

>>> m=re.match( rule , target )

>>> if m: #

doSomethin

match search

match MatchObject Python


>>> s= 'Tom:9527 , Sharry:0003'

>>> m=re.match( r'(?P<name>/w+):(?P<num>/d+)' , s )

>>> m.group()

'Tom:9527'

>>> m.groups()

('Tom', '9527')

>>> m.group(name)

'Tom'

>>> m.group(num)

'9527'

2.3 finditer

finditer( rule , target [,flag] )

findall

finditer findall findall finditer

>>> s=111 222 333 444

>>> for i in re.finditer(r/d+ , s ):

print i.group(),i.span() #

111 (0, 3)

222 (4, 7)

333 (8, 11)

444 (12, 15)

finditer for i in finditer()

Match

2.4

re

sub ( rule , replace , target [,count] )

subn(rule , replace , target [,count] )

sub

sub

dog cat

>>> s= I have a dog , you have a dog , he have a dog

>>> re.sub( rdog , cat , s )

' I have a cat , you have a cat , he have a cat '

>>> re.sub( rdog , cat , s , 2 )

' I have a cat , you have a cat , he have a dog '

subn

>>> re.subn( rdog , cat , s )


(' I have a cat , you have a cat , he have a cat ', 3)

split( rule , target [,maxsplit] )

str split ,

>>> s= I have a dog , you have a dog , he have a dog

>>> re.split( /s*,/s* , s )

[' I have a dog', 'you have a dog', 'he have a dog ']

str split ,

escape( string )

non-alphanumerics

re

s (*+?)

>>> s= 111 222 (*+?) 333

>>> rule= re.escape( r(*+?) )

>>> print rule

/(/*/+/?/)

>>> re.findall( rule , s )

['(*+?)']

3 re

Python match match

patter

3.1 Pattern

compile

Pattern re findall , match , search

,finditer , sub , subn , split re

Pattern re

findall , match , search finditer

findall ( targetString [, startPos [,endPos] ] )

finditer ( targetString [, startPos [,endPos] ] )

match ( targetString [, startPos [,endPos] ] )

search ( targetString [, startPos [,endPos] ] )

re

re Pattern

flags

pattern

groupindex

>>> p=re.compile( r'(?P<word>/b[a-z]+/b)|(?P<num>/b/d+/b)|(?P<id>/b[a-z_]+/w*/b)' , re.I )

>>> p.flags

>>> p.pattern

'(?P<word>//b[a-z]+//b)|(?P<num>//b//d+//b)|(?P<id>//b[a-z_]+//w*//b)'

>>> p.groupindex

{'num': 2, 'word': 1, 'id': 3}

ID word , num id

p p.flag I 2

re.I 2

>>> re.I

>>> re.L

>>> re.M

p.pattern print

>>> print p.pattern

(?P<word>/b[a-z]+/b)|(?P<num>/b/d+/b)|(?P<id>/b[a-z_]+/w*/b)

p.groupindex key values

key

3.2 Match

Match Python Match Python

3.2.1

1 re 0

>>> p=re.compile( r(?P<name>[a-z]+)/s+(?P<age>/d+)/s+(?P<tel>/d+).* , re.I )

>>> p.groupindex

{'age': 2, 'tel': 3, 'name': 1}

>>> s=Tom 24 88888888 <=

>>> m=p.search(s)

>>> m.groups() #

('Tom', '24', '8888888')

>>> m.group(name) #

Tom

>>> m.group( 1 ) #

>>> m.group(0) # 0

'Tom 24 88888888 <='

0 , 0 m.group(0)

m.group()
>>> m.group()

'Tom 24 88888888 <='

Match

3.2.2 Match

group([index|id]) 0,

groups()

groupdict() key values

>>> m.groupindex()

{'age': '24', 'tel': '88888888', 'name': 'Tom'}

start( [group] )

end( [group] )

span( [group] )

expand( template )

/g<index|name> /index

>>> m.expand(r'name is /g<1> , age is /g<age> , tel is /3')

'name is Tom , age is 24 , tel is 88888888'

Match

pos

endpos

findall match

0,

>>> m.pos

>>> m.endpos

19

lastindex

>>> m.lastindex

lastgroup

>>> m.lastgroup

'tel'

re Pattern

>>> m.re.pattern

'(?P<name>[a-z]+)//s+(?P<age>//d+)//s+(?P<tel>//d+).*'

string

>>> m.string

'Tom 24 88888888 <='

4
Python Python

Python

Python

Python

http://wiki.ubuntu.org.cn/index.PHP?

title=Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93%8D%E4%BD%9C%E6%8C%87%
cn


1 0

Listbox

HTTPURL

.NET PHP
1914 | 805 4050 | 690

Python
15335 | 1230

Python --IO day2 shell


Python --- day3 Machine Learning&Deep Learning
Python kingofcoderscom
Python Python
Python python

1 kljxy 2015-07-11 21:05

\\d\s\w/
http://blog.csdn.net/michaelpp/article/details/9631979
http://datahub.top/coursedetail?id=7

,[][]

* CSDN

Hadoop AWS Java Android iOS Swift Docker OpenStack


VPN Spark ERP IE10 Eclipse CRM JavaScript Ubuntu NFC WAP jQuery
BI HTML5 Spring Apache .NET API HTML SDK IIS Fedora XML LBS Unity
Splashtop UML components Windows Mobile Rails QEMU KDE Cassandra CloudStack
FTC coremail OPhone CouchBase iOS6 Rackspace Web App SpringSide
Maemo Compuware aptech Perl Tornado Ruby Hibernate ThinkPHP HBase
Pure Solr Angular Cloud Foundry Redis Scala Django Bootstrap

| | | | | | | | |

webmaster@csdn.net 400-600-2320 | |

ICP 09002463 | Copyright 1999-2016, CSDN.NET, All Rights Reserved