How to use? and ?: and: in REGEX for Python?

I understand that

* = "zero or more"
? = "zero or more" ...what the difference?

In addition, & alpha ;: <<my book uses this, he talks about his "subtlety", but I don’t know what exactly they do!

+3
source share
4 answers

As Manu already said, it ?means "zero or once." This is the same as {0,1}.

?:, , , (?:X), X - . " ". , -, , . , .(.).(.) 4 ( ) 1 2. , : .(?:.).(.) 1, , (?:.), , "".

:

import re
m = re.search('.(.).(.)', '1234')
print m.group(1)
print m.group(2)
# output:
# 2
# 4

m = re.search('.(?:.).(.)', '1234')
print m.group(1)
# output:
# 4

: " , ?". , OR , , "www.google.com" "www.yahoo.com", : www\.google\.com|www\.yahoo\.com, : www\.(google|yahoo)\.com, . - , ( "google" "yahoo" ), : www\.(?:google|yahoo)\.com. "" "google" "yahoo", /script . , , (-) , , , .

, , . .

+5
< < < , "", , !

, , .

(: ) ? . , . ?: - . , , :

a(.)c

, , a c. ( ) . , :

m = re.search('a(.)c', 'abcdef')
print m.group(1)

b, m.group(1) (group(0) , abc).

:

a(?:.)c

- , ?: . :

print m.group(1)

1!

+4

? =

(?:) , ()

+2

? " ", " ".

+1

Source: https://habr.com/ru/post/1720261/


All Articles