Remove a regular expression specific string

Asked 1 years ago, Updated 1 years ago, 40 views

Question 1: (Solution)

[Pure Fore] Alice Total Cream 50 ml

[Glossy Blossom] One nail sticker, Vivid Shell

[Apieu] iMousse. [#3 Maple Toast] 6g

I want to erase the brand name first

brand = ["Pure Fore", "Glossy Blossom", "Apieu"]
target.replace("[", "").replace(brand, "").replace("]", "")

If you do this, Apeu disappears at the back, too Is there a way to erase [brand name] using the regular expression? It doesn't have to be a regular expression!

+

print (re.compile('\[A-za-z is-he]+\]').sub('',target))

Number one is working like this.

If there is any other way to do it, please let me know

Question 2:

A

[Apieu] iMousse [No.4_Ginger Cream] 6g

[Apieu] i-mousse [No.5_Daily Mocha] 6g

B

[APIEU] iMUS 

[Apieu] I Mousse 

Is there a way to make A look like B by leaving only the words that match the beginning and erasing the words in the end?

python regex

2022-09-21 12:16

1 Answers

I made the brand name based on the fact that it is always located at the top of the line.

"[Brand name] Blah blah"

Code to remove only [] at the beginning of the line.

import re

text ="[Pure Fore] Alice Total Cream 50 ml
[Glossy Blossom] One nail sticker, Vivid Shell
[Apieu] i-mousse. [#3 Maple Toast] 6g'""

print(re.sub('^\[\w+\] ', '', text, flags=re.MULTILINE))

# Output Results
#Alice Total Cream 50 ml
#NailSticker Vivid Shell 1 ea
#Immus [No. 3_Maple Toast] 6 g

Code to find matches from the beginning of a sentence.

In the example you suggested, it would be [Apieu] Imitus [.

difflib library was used. (Default library)

from difflib import SequenceMatcher

string1 = "[Apieu] iMousse [No.4_Ginger Cream] 6g"
string2 = "[Apieu] iMousse [No.5_Daily Mocha] 6g"

match = SequenceMatcher(None, string1, string2).find_longest_match(0, len(string1), 0, len(string2))

print(string1[match.a:match.a+match.size]) # "[Apieu] iMousse []"

The above code was created by referring to here (actually imported).)

I hope it was helpful!


2022-09-21 12:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.