0

I've been looking for a way to isolate special characters in a regex expression, but I only seem to find the exact opposite of what I'm looking for. So basically I want to is something along the lines of this:

import re
str = "I only want characters from the pattern below to appear in a list ()[]' including quotations"

pattern = """(){}[]"'-"""
result = re.findall(pattern, str)

What I expect from this is:

print(result)
#["(", ")", "[", "]", "'"]
Dharman
  • 30,962
  • 25
  • 85
  • 135
osmark86
  • 11
  • 1
  • 5

4 Answers4

0

Why would you need regex for this when it can be done without regex?

>>> str = "I only want characters from the pattern below to appear in a list ()[]' including quotations"
>>> pattern = """(){}[]"'-"""
>>> [x for x in str if x in pattern]
['(', ')', '[', ']', "'"]
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0

If it's for learning purposes (regex isn't really the best way here), then you can use:

import re

text = "I only want characters from the pattern below to appear in a list ()[]' including quotations"
output = re.findall('[' + re.escape("""(){}[]"'-""") + ']', text) 
# ['(', ')', '[', ']', "'"]

Surrounding the characters in [ and ] makes it a regex character class and re.escape will escape any characters that have special regex meaning to avoid breaking the regex string (eg: ] terminating the characters early or - in a certain place causing it to act like a character range).

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
0

Several of the characters in your set have special meaning in regular expressions; to match them literally, you need to backslash-escape them.

pattern = r"""\(\)\{\}\[]"'-"""

Alternatively, you could use a character class:

pattern = """[]-[(){}"']"""

Notice also the use of a "raw string" r'...' to avoid having Python interpret the backslashes.

tripleee
  • 175,061
  • 34
  • 275
  • 318
-1

This regex that solved my problem:

pattern = r"""[(){}\[\]"'\-]"""

Posted on behalf of the question asker

Dharman
  • 30,962
  • 25
  • 85
  • 135