简体   繁体   中英

Regex multiple occurrences of same character Python

I'm having a problem with isolating a dynamic string+int occurrence in a text:

I want to capture "k9034", the first char is always a string, the length of the following int can vary in length "9034...76"

Given:

K:\dir1\executions\ is static and always the same the number of \ is always the same in the full text

So far I have made a script:

^K.*executions\\([a-t])

It captures K:\dir1\executions\ in match 1 and k in group 1

Since k9034 varies in length I would like to write something like:

^K.*executions\\([a-t].*)\\.*

For "\. " I would like to capture the first \ after k9034 and put it in a match or other group(\. ), but with my script it captures the wrong \

Im using regex101.com to test it.

K:\dir1\executions\k9034\kejlk34f\fdshf3\

Best regards

H

You could write it either using 2 capture groups:

^K:\\[^\s\\]+\\executions(\\[a-t]\d+)(\\)

Regex demo

Or use a single capture group

^K:\\[^\s\\]+\\executions(\\[a-t]\d+\\)

Explanation

  • ^ Start of string
  • K:\\ Match K:\
  • [^\s\\]+ Match 1+ chars other than \ or whitespace chars
  • \\executions Match \executions
  • (\\[at]\d+\\) Capture group 1 , match \ then a single char in the range at and 1+ digits

Regex demo

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