简体   繁体   中英

Regex: match certain word in string enclosed by parentheses

I'm scraping the window title of a program to get the current state using python. The title of the targeted window is formatted like this:

[File1.xyz] (Saved file1.xyz)

I want to match the "Saved" but ignore the rest of it, but have to make sure it's in the parentheses.

My approached looks like this

\[([^)]+)\] \((\bSaved\b)\)

but it only matches (Saved) and not (Saved file1.xyz).

Edit: The program is processing a file and displays the current progress in the title bar. I want to match "Saved" to determine that the program is finished, in this case the window title would be: (Saved untitled.stl) It's not always the first word so I can't use this to match it.

Your regex has no expression for the actual filename in the second part. I think you want to do this:

\[([^)]+)\] \((Saved)[^)]+\)

This would give File1.xyz for the first and Saved for the second group of the match.

See here for yourself.

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