Search code examples
pythonregexpython-re

Find whole word for matching


I am trying to get the regex pattern to find whole word if it matches a word\special character

text = Details of Test-3K9Y9Y1-My-Node1 give me

I want to get Test-3K9Y9Y1-My-Node1 from the sentence.

I tried:

>>> match = re.findall('(?<=-)\w+', text)
>>> match
['3K9Y9Y1', 'My', 'Node1']

Solution

  • You could try to use the following regular expression:

    >>match = re.findall('[^ ]*-[^ ]*', text)
    

    And you could try it out here