Python Forum
Regex - Pass Flags as a function argument?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex - Pass Flags as a function argument?
#1
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
Reply
#2
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__)
Reply
#3
Brick 
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__)
Reply
#4
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__)
Reply
#5
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__)
Reply
#6
(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.
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mutable argument in function definition akbarza 1 486 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  How to pass encrypted pass to pyodbc script tester_V 0 865 Jul-27-2023, 12:40 AM
Last Post: tester_V
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,100 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,863 Nov-06-2022, 04:28 AM
Last Post: Skaperen
Question How to pass a method as argument in an another method? anilanvesh 6 2,751 Sep-30-2021, 10:18 PM
Last Post: deanhystad
  Passing flags to python script, through a function xbit 4 3,985 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  Confused with 'flags' tester_V 10 4,924 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Possible to dynamically pass arguments to a function? grimm1111 2 2,197 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  How to use a tuple as an argument of a function zarox 5 3,626 Nov-14-2020, 08:02 PM
Last Post: buran
  calling a function and argument in an input phillup7 3 2,619 Oct-25-2020, 02:12 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020