Python

Python Regex

re.match

re.match match the pattern from the start of the string

1
re.match(pattern, string, flags=0)

re.search match the pattern of the whole string

1
re.search(pattern, string, flags=0)

Use group(num=0) to get the result and groups() will return a tuple

search and replace

Replace the substring matched by pattern to the rep1

1
re.sub(pattern, rep1, string, count=0)

parameters:

  • rep1 is the new string or a function to replace the original substring
  • count is the max number of doing replace

compile

compile can generate a Regex object which provides match() and ‘search()’ funcitons.

1
re.compile(pattern[, flags])

For the match object:

  • group() can get different matched substring
  • start([group]) get the index of the first character in the substring ([group] is the index of group whose default value is 0)
  • end([group]) get the index of the final character in the substring
  • span([group]) return (start([group], end([group])))

findall

Find all the substring matched and return a list.

match and search only match once.

1
findall(string[,pos[,endpos]])
  • pos optional parameter, the start index of the string and default value is 0
  • endpos optional parameter, the end index of the string and default calue is the length of string

re.finditer

Find all the substring matched and return a iterator.

1
re.finditer(pattern, string, flags=0)

re.split

Split the string by the matched substring

1
re.split(pattern, string[, maxsplit=0, floags=0])