Home > Software design >  To Find data inside tag how to write REGEX Expression?
To Find data inside tag how to write REGEX Expression?

Time:08-10

I need to find data inside tag:

data="<SHORT-NAME>WhlDist_Prtctd_PDU_PA_CAN_1_controller</SHORT-NAME>"

I tried this

print(re.search('<SHORT-NAME>("^WhlDist\w.*controller$")<\/\1>', data))

but output is "None", expected output is

WhlDist_Prtctd_PDU_PA_CAN_1_controller

CodePudding user response:

You need

match = re.search(r'<SHORT-NAME>(WhlDist\w controller)</SHORT-NAME>')
if match:
    print( match.group(1) )

See the regex demo.

Details:

  • <SHORT-NAME> - a fixed string
  • (WhlDist\w controller) - Group 1: WhlDist, one or more word chars, controller
  • </SHORT-NAME> - a fixed string.
  • Related