简体   繁体   中英

Python RegEx: How to remove all characters to the left of and including a given character

I need to remove every character to the left of the first "[" bracket, as well as the bracket itself. All I want to remain are the host ID's. Can someone help me out and show me a way to do this in RegEx?

 warnings.warn(
resources: [
  b484e9aa2xxxe95a3225,
  9a854531xxx2a4f84394c,
  e127d90xxxe0825d7c949,
  3252014xxxd8568b4Gsd3,

Use a regular expression that matches everything up to the first [ character, and replace it with an empty string.

text = re.sub(r'^.*?\[', '', text, flags=re.DOTALL)

The re.DOTALL flag makes .* match across newlines. And the ? after .* makes it non-greedy, so it stops at the first [ rather than the last one.

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