Posts: 138
Threads: 26
Joined: Nov 2020
Sep-04-2021, 06:48 PM
(This post was last modified: Sep-05-2021, 11:03 AM by muzikman.)
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
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-04-2021, 09:02 PM
(This post was last modified: Sep-06-2021, 03:41 PM by snippsat.)
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
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__)
Posts: 138
Threads: 26
Joined: Nov 2020
Sep-05-2021, 11:05 AM
(This post was last modified: Sep-05-2021, 11:05 AM by muzikman.)
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
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__)
Posts: 138
Threads: 26
Joined: Nov 2020
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
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__)
Posts: 138
Threads: 26
Joined: Nov 2020
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
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__)
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-06-2021, 03:42 PM
(This post was last modified: Sep-06-2021, 03:48 PM by snippsat.)
(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.
Posts: 138
Threads: 26
Joined: Nov 2020
(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.
|