Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print regex match results in python 3?

I was in IDLE, and decided to use regex to sort out a string. But when I typed in what the online tutorial told me to, all it would do was print:

<_sre.SRE_Match object at 0x00000000031D7E68>

Full program:

import re
reg = re.compile("[a-z]+8?")
str = "ccc8"
print(reg.match(str))

result:

<_sre.SRE_Match object at 0x00000000031D7ED0>

Could anybody tell me how to actually print the result?

like image 876
Pythonic Avatar asked Oct 18 '14 09:10

Pythonic


People also ask

What does regex match return Python?

The Python RegEx Match method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object. But if a match is found in some other line, the Python RegEx Match function returns null.

How do I print a string in regex?

How to print all the characters of a string using regular expression in Java? Compile the regular expression using the compile() method. Create a Matcher object using the matcher() method. Find the matches using the find() method and for every match print the matched contents (characters) using the group() method.

How to match a regex pattern inside a string in Python?

Python re.match () method looks for the regex pattern only at the beginning of the target string and returns match object if match found; otherwise, it will return None. In this article, You will learn how to match a regex pattern inside the target string using the match (), search (), and findall () method of a re module.

How do you check if a string matches a regular expression?

Regex with capturing group. If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

How to use regular expressions with regex in Python?

RegEx can be used to check if a string contains the specified search pattern. Python has a built-in package called re, which can be used to work with Regular Expressions. When you have imported the re module, you can start using regular expressions: The re module offers a set of functions that allows us to search a string for a match:

How do you search for a pattern in regex?

The re.search () method takes two arguments: a pattern and a string. The method looks for the first location where the RegEx pattern produces a match with the string. If the search is successful, re.search () returns a match object; if not, it returns None.


2 Answers

You need to include .group() after to the match function so that it would print the matched string otherwise it shows only whether a match happened or not. To print the chars which are captured by the capturing groups, you need to pass the corresponding group index to the .group() function.

>>> import re
>>> reg = re.compile("[a-z]+8?")
>>> str = "ccc8"
>>> print(reg.match(str).group())
ccc8

Regex with capturing group.

>>> reg = re.compile("([a-z]+)8?")
>>> print(reg.match(str).group(1))
ccc

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

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

Note that even in MULTILINE mode, re.match() will only match at the beginning of the string and not at the beginning of each line.

like image 134
Avinash Raj Avatar answered Sep 21 '22 19:09

Avinash Raj


If you need to get the whole match value, you should use

m = reg.match(r"[a-z]+8?", text)
if m:                          # Always check if a match occurred to avoid NoneType issues
  print(m.group())             # Print the match string

If you need to extract a part of the regex match, you need to use capturing groups in your regular expression. Enclose those patterns with a pair of unescaped parentheses.

To only print captured group results, use Match.groups:

Return a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.

So, to get ccc and 8 and display only those, you may use

import re
reg = re.compile("([a-z]+)(8?)")
s = "ccc8"
m = reg.match(s)
if m:
  print(m.groups()) # => ('ccc', '8')

See the Python demo

like image 30
Wiktor Stribiżew Avatar answered Sep 23 '22 19:09

Wiktor Stribiżew