Regular expression parentheses effect, the form "(\ w +) \ s + \ w +"

  1. import re
  2. string= "2345 3456 4567 5678"
  3. regex=re.compile( "(\w+)\s+\w+")
  4. print(regex.findall(string))
  5. #['2345', '4567']

Look at an example:

   import re

   string = “2345 3456 4567 5678”

   regex = re.compile("(\w+)\s+\w+")

 print(regex.findall(string))

   (Output is: [ '2345', '4567'])

There is a regular expression in parentheses, the contents of the output result is that the brackets to match the content, rather than to the entire expression matched, but the entire regular expression is executed, but only to the content output matching brackets, in Like the first match when said bracket is not matched to "23453456", except that only the output (/ w +) i.e. the matched result of "2345", the second matching Similarly from "4567" to match one to "4567 5678", but still only output "4567"


Guess you like

Origin www.cnblogs.com/xiaohaodeboke/p/11777984.html