Python Forum
Regex - Pass Flags as a function argument? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Regex - Pass Flags as a function argument? (/thread-34831.html)



Regex - Pass Flags as a function argument? - muzikman - Sep-04-2021

Greetings,

I am just playing around with possibly making some methods for my own class for regex. I have a slight problem. Take a look at the search function and tell me how I would add the flags as a parameter for that function.
flags = re.VERBOSE | re.IGNORECASE 
def search(pattern, string): # How can I pass the flags as a parameter to this function?
    
    result = re.search(pattern, string, flags = re.VERBOSE | re.IGNORECASE) # I want to pass these as an argument to the 'search' function.
    
    if result is None:
        return None
    else:
        return result

def regex(func, pattern, string):
    return func(pattern, string)


string = "This is a string with two phone numbers 555-555-5555 and 555-555-5555"
pattern = r"\d{3}-\d{3}-\d{4}"

result = regex(search, pattern, string)
print(result)
Thank you
Matt


RE: Regex - Pass Flags as a function argument? - snippsat - Sep-04-2021

Like this,so if look regex source code they do same with flags=0.
This is kind of double up just use the search function Confused
import re

def search(pattern, string, flags=0):
    result = re.search(pattern, string, flags) # I want to pass these as an argument to the 'search' function.
    if result is None:
        return None
    else:
        return result

if __name__ == '__main__':
    string = "This is a string with two phone numbers 555-555-5555 and 555-555-5555"
    pattern = r"\d{3}-\d{3}-\d{4}"
    flags = re.VERBOSE | re.IGNORECASE
    result = search(pattern, string, flags)
    print(result)
The argument given has to be same format then get parsing | for free.
Them do some magic to join | in parameter,just to take out 2 lines.
res = '|'.join(members)
globals().update(RegexFlag.__members__)



RE: Regex - Pass Flags as a function argument? - muzikman - Sep-05-2021

You're correct. There is some redundancies here and that's the first indication that I should stay away for it. I was playing with passing functions as arguments and just wanted to see if it was possible.

Thanks for your help. I just realized. The numbers in the string area real. So, I need to get rid of them. Could you edit your post and remove them and I will do the same? Or as Admin, delete it; if you feel there is no value to the post.

Thank you

(Sep-04-2021, 09:02 PM)snippsat Wrote: Like this,so if look regex source code they do same with flags=0.
This is kind of double up just use the search function Confused
import re

def search(pattern, string, flags=0):
    result = re.search(pattern, string, flags) # I want to pass these as an argument to the 'search' function.
    if result is None:
        return None
    else:
        return result

if __name__ == '__main__':
    string = "This is a string with two phone numbers 555-555-5555 and 555-555-5555"
    pattern = r"\d{3}-\d{3}-\d{4}"
    flags = re.VERBOSE | re.IGNORECASE
    result = search(pattern, string, flags)
    print(result)
The argument given has to be same format then get parsing | for free.
Them do some magic to join | in parameter,just to take out 2 lines.
res = '|'.join(members)
globals().update(RegexFlag.__members__)



RE: Regex - Pass Flags as a function argument? - muzikman - Sep-06-2021

Can you please remove the phone numbers from your post. They are real phone numbers. I forgot to remove them before posting.

Thanks.

(Sep-04-2021, 09:02 PM)snippsat Wrote: Like this,so if look regex source code they do same with flags=0.
This is kind of double up just use the search function Confused
import re

def search(pattern, string, flags=0):
    result = re.search(pattern, string, flags) # I want to pass these as an argument to the 'search' function.
    if result is None:
        return None
    else:
        return result

if __name__ == '__main__':
    string = "This is a string with two phone numbers "
    pattern = r"\d{3}-\d{3}-\d{4}"
    flags = re.VERBOSE | re.IGNORECASE
    result = search(pattern, string, flags)
    print(result)
The argument given has to be same format then get parsing | for free.
Them do some magic to join | in parameter,just to take out 2 lines.
res = '|'.join(members)
globals().update(RegexFlag.__members__)



RE: Regex - Pass Flags as a function argument? - muzikman - Sep-06-2021

Please remove phone numbers.

(Sep-04-2021, 09:02 PM)snippsat Wrote: Like this,so if look regex source code they do same with flags=0.
This is kind of double up just use the search function Confused
import re

def search(pattern, string, flags=0):
    result = re.search(pattern, string, flags) # I want to pass these as an argument to the 'search' function.
    if result is None:
        return None
    else:
        return result

if __name__ == '__main__':
    string = "This is a string with two phone numbers "
    pattern = r"\d{3}-\d{3}-\d{4}"
    flags = re.VERBOSE | re.IGNORECASE
    result = search(pattern, string, flags)
    print(result)
The argument given has to be same format then get parsing | for free.
Them do some magic to join | in parameter,just to take out 2 lines.
res = '|'.join(members)
globals().update(RegexFlag.__members__)



RE: Regex - Pass Flags as a function argument? - snippsat - Sep-06-2021

(Sep-06-2021, 02:18 PM)muzikman Wrote: Please remove phone numbers.
Phone numbers is removed.
(Sep-05-2021, 11:05 AM)muzikman Wrote: Or as Admin, delete it; if you feel there is no value to the post.
We do not delete post if not spam or other silliness,it can be value for someone we try not to interfere in that decision.


RE: Regex - Pass Flags as a function argument? - muzikman - Sep-06-2021

(Sep-06-2021, 03:42 PM)snippsat Wrote:
(Sep-06-2021, 02:18 PM)muzikman Wrote: Please remove phone numbers.
Phone numbers is removed.

Thank you.