Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can re do this?
#11
i mean an argument on the call to the re method. something in the regex that means to use the 3rd argument of the method call. i thought it might be problematic to insert arbitrary characters into regexs.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#12
(Feb-07-2022, 04:44 PM)Skaperen Wrote: i mean an argument on the call to the re method. something in the regex that means to use the 3rd argument of the method call. i thought it might be problematic to insert arbitrary characters into regexs
No this is not problem at all.
Reply
#13
is there a way to make a regex that means to get data from somewhere? or is it all about making a complete regex?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
The correct way to insert arbitrary characters in a regex is to escape them with re.escape()
>>> import re
>>> chars = ['\\', '{', 'ᎈ', 'ᎉ']
>>> s = re.escape(''.join(chars))
>>> pat = f'[{s}]'
>>> re.split(pat, 'foo\\bar{spam}eggs')
['foo', 'bar', 'spam}eggs']
Skaperen likes this post
Reply
#15
so, now, i need to find how to do this in reverse such that my set of characters are the ones i want in the result with all others being the separators. e.g. match everything except ...
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#16
(Feb-12-2022, 02:05 AM)Skaperen Wrote: reverse such that my set of characters are the ones i want in the result with all others being the separators. e.g. match everything except ...
>>> import re
>>> 
>>> s = 'hello bus and cab 🤨'
>>> r = re.split(r'[🤨hello]', s)
>>> [x.strip() for x in r if x]
['bus and cab']
>>> 
>>> r = re.split(r'[^🤨hello]', s)
>>> [x.strip() for x in r if x]
['hello', '🤨']
Reply


Forum Jump:

User Panel Messages

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