简体   繁体   中英

Python regex for words with a series of character

I am relatively new to Python regex and have a problem which I could not solve yet.

Problem :

I have strings that contain characters ( | , [ , } , { , \n ) along with alphanumeric characters.

From the following examples, I am trying to extract only frank_one-123_gold.jpg and 45frank_one-123_gold.JPG at the same (as example 1 and 2 may be in the same sentence).

Example1 :

timecrypto|<br>file:_team.svg|[[File:apple.svg]]frank_one-123_gold.jpg

Example2 :

timecrypto|file:_team.svg|[[File:apple.svg_\n\n45frank_one-123_gold.JPG<gallerg/>

You can use "re" lib:

import re

file_name = "_team.svg|[[File:apple.svg]]frank_one-123_gold.jpg"
file_name_filtered = re.split('\]\]|\n',file_name)[-1]

Here we use "\]\]|\n" pattern, where this split the name into the characters "\]\]" or ("or" represented as "|") "\n".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM