The topic of regular expression
Ask, the answer is?
Why is the answer?
CodePudding user response:
The answer is in my heart comIt's a pity that no option
CodePudding user response:
ifcode.comCodePudding user response:
Complete matching 0-14 www.ifcode.comGroup 1. 0 to 14 www.ifcode.com
Group 2. 0 to 3 WWW
Group 3. 4-14 ifcode.com
Group 4. 4-10 ifcode
Group 5. 11-14 com
Options: A
CodePudding user response:
Short answer: choose A=ifcode.comA:
((WWW). ((ifcode.) (com)))
_______ | | the... 0 0, the default is matching the whole, complete, all of the string
Count from left to right is which a left parenthesis:
1 _______ | | the... the first bracket is group (1)
Three _____ | | _2_ | |... the second bracket and the scope of the third parentheses, respectively, as shown in the left
| ___4__ | | _5_ | the scope of the fourth and fifth brackets brackets, respectively, as shown in the left
- & gt; So here's group (3) corresponds to:
((ifcode.) (com))
Parts, namely:
Ifcode.com
Also can use tools to see more clearly:
RegExr: Learn, Build, & amp; Test the RegEx
Comments:
Ask so out, is to want to test you:
(1) know the default group (0), always said is all the result of the match, or know the existence of the group (0)
(2) to investigate group calculation rules, you are right number from left to right, the minimum is 1, each left parenthesis (it corresponds to the serial number of the group
Depth evaluation:
In fact, the above this kind of writing, it is not good to write
Should use the name group to write, logic clearer
The complete code is as follows:
The import re
InputDomainStr="www.crifan.com"
(domainP="? P(? P WWW). (? P (? P Crifan). (? P Com)))
"FoundDomain=re search (domainP inputDomainStr)
If foundDomain:
WholeMatchStr=foundDomain. Group (0)
Print (" wholeMatchStr=% s "% wholeMatchStr)
WholeDomain=foundDomain. Group (" wholeDomain ")
Print (" wholeDomain=% s "% wholeDomain)
SubDomain=foundDomain. Group (" subDomain ")
Print (" subDomain=% s "% subDomain)
HostOrg=foundDomain. Group (" hostOrg ")
Print (" hostOrg=% s "% hostOrg)
The host=foundDomain. Group (" host ")
Print (" host=% s "% host)
Org=foundDomain. Group (" org ")
Print (" org=% s "% org)
# wholeMatchStr=www.crifan.com
# wholeDomain=www.crifan.com
# subDomain=WWW
# hostOrg=crifan.com
# host=crifan
# org=com
Print ("="* 80)
Further optimization:
(1)
The above points, is able to match any character, and not just point itself, but here is meant only matching point itself, so should be optimized for \.
(2) match more domain name
If you want to match to other more domains, such as:
Book.crifan.com
www.crifan.org
Wait for a circumstance, you can use the following code:
InputDomainList=[
"Www.crifan.com",
"Book.crifan.com",
"Wiki.crifan.com",
"Www.crifan.net",
"Www.crifan.org",
]
For curIdx eachDomainStr enumerate in (InputDomainList) :
Print (" % s % s % s "% (" -" * 20, curIdx, "-" * 20))
Print (" eachDomainStr=% s "% eachDomainStr)
(multiDomainP="? P(? P The \ w +). (? P (? P The \ w +). (? P The \ w +)))
"FoundMultiDomain=re search (multiDomainP eachDomainStr)
If foundMultiDomain:
CurWholeMatchStr=foundMultiDomain. Group (0)
Print (" curWholeMatchStr=% s "% curWholeMatchStr)
CurWholeDomain=foundMultiDomain. Group (" wholeDomain ")
Print (" curWholeDomain=% s "% curWholeDomain)
CurSubDomain=foundMultiDomain. Group (" subDomain ")
Print (" curSubDomain=% s "% curSubDomain)
CurHostOrg=foundMultiDomain. Group (" hostOrg ")
Print (" curHostOrg=% s "% curHostOrg)
CurHost=foundMultiDomain. Group (" host ")
Print (" curHost=% s "% curHost)
CurOrg=foundMultiDomain. Group (" org ")
Print (" curOrg=% s "% curOrg)
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# eachDomainStr=www.crifan.com
# curWholeMatchStr=www.crifan.com
# curWholeDomain=www.crifan.com
# curSubDomain=WWW
# curHostOrg=crifan.com
# curHost=crifan
# curOrg=com
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# eachDomainStr=book.crifan.com
# curWholeMatchStr=book.crifan.com
# curWholeDomain=book.crifan.com
# curSubDomain=the book
# curHostOrg=crifan.com
# curHost=crifan
# curOrg=com
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 2 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# eachDomainStr=wiki.crifan.com
# curWholeMatchStr=wiki.crifan.com
# curWholeDomain=wiki.crifan.com
# curSubDomain=wiki
# curHostOrg=crifan.com
# curHost=crifan
# curOrg=com
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 3 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# eachDomainStr=www.crifan.net
# curWholeMatchStr=www.crifan.net
# curWholeDomain=www.crifan.net
# curSubDomain=WWW
# curHostOrg=crifan.net
# curHost=crifan
# curOrg=net
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 4 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# eachDomainStr=www.crifan.org
# curWholeMatchStr=www.crifan.org
# curWholeDomain=www.crifan.org
# curSubDomain=WWW
# curHostOrg=crifan.org
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull