Home > Net >  How to find text between `*~`?
How to find text between `*~`?

Time:01-24

There are lots of special characters in these text file I get and I am trying to find and replace them with other strings. For example:

s = "BRAND~*HP*5250*RAM128*GPUAMD7990*~"

df = re.findall('BRAND~*(. ?)*~', s)

print(df)

The result yield ['*']

I want the result to be HP*5250*RAM128*GPUAMD7990

How do I achieve this?

CodePudding user response:

Escape the * in the pattern:

s = "BRAND~*HP*5250*RAM128*GPUAMD7990*~"

out = re.findall('BRAND~\*(. ?)\*~', s)

Output:

['HP*5250*RAM128*GPUAMD7990']

CodePudding user response:

You need to escape the '*'s:

df = re.findall('BRAND~\*(. ?)\*~', str)

CodePudding user response:

You want to

  1. match the * asterisk as a normal 'character, and
  2. strip the remaining *

PS: I see your question has been edited. You may discard the 'strip' part. It seems you did not need that anymore.

[match / find]

import re

## find match based on regex pattern
df = re.findall('BRAND~\*(. ?)\*~', str)
df

[out] ['HP*5250*RAM128*GPUAMD7990']

[strip / remove]

r = re.compile('(\*)')
[r.sub('', w) for w in df]

out ['HP5250RAM128GPUAMD7990']


PS: Others might assist with a more python code!

PS: You may want to take note of Find and replace string values in list [duplicate]

CodePudding user response:

A pair of lookarounds will get the center.

(?<=BRAND~\*).*?(?=\*~)

https://regex101.com/r/zdkmYD/1

But to replace specific characters within that match requires a callback function.

  • Related