Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return string representation of re.search() in python

Tags:

python

regex

I am aware that [re.search(pattns,text)][1] in python method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise.

My problem however is, am trying to implement this using OOP (class) i want to return a string representation of the results of the matches whether true or none or any other form of representation(readable) not this <__main__.Expression instance at 0x7f30d0a81440> below are two example classes : Student and Epression. The one using __str__(self)__ works fine but i cannot figure out how to get the representation funtion for re.search(). Please someone help me out.

import re   

class Expression:
    def __init__(self,patterns,text):

        self.patterns = patterns
        self.text = text


    def __bool__(self):
    # i want to get a readable representation from here
        for pattern in self.patterns:
           result = re.search(pattern,self.text)
           return result



 patterns = ['term1','term2','23','ghghg']
 text = 'This is a string with term1 23 not ghghg the other'

 reg = Expression(patterns,text)
 print(reg)


 class Student:

    def __init__(self, name):

       self.name = name


   def __str__(self):
    # string representation here works fine
        result = self.name 
        return result

 # Usage:

  s1 = Student('john')
  print(s1)


 [1]: https://developers.google.com/edu/python/regular-expressions
like image 257
Seyyid Said Avatar asked Apr 28 '18 06:04

Seyyid Said


People also ask

Does re search return a string?

While re. findall() returns matches of a substring found in a text, re. match() searches only from the beginning of a string and returns match object if found. However, if a match is found somewhere in the middle of the string, it returns none.

What is the output of re search in Python?

However, re.search() only returns the first match. The lower case letter pattern matches: The sequence of letters at the beginning of the string. The zero-width spot between the 1 and 2.

How do I replace a string with re?

To replace a string in Python, the regex sub() method is used. It is a built-in Python method in re module that returns replaced string. Don't forget to import the re module. This method searches the pattern in the string and then replace it with a new given expression.


1 Answers

The output of re.search returns a match object. It tells you whether the regex matches the string.

You should identify the group to retrieve string from the match like so:

if result: 
    return result.group(0)

Replace return result in your code with above code snippet.

If you are not sure how group works, here is an example from docs:

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist") 
>>> m.group(0) # The entire match 
'Isaac Newton' 
>>> m.group(1) # The first parenthesized subgroup. 
'Isaac' 
>>> m.group(2) # The second parenthesized subgroup. 
'Newton' 
>>> m.group(1, 2) # Multiple arguments give us a tuple. 
('Isaac', 'Newton')
like image 193
Austin Avatar answered Oct 23 '22 01:10

Austin