Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract substring from dot untill colon with Python regex

I have a string that resembles the following string:

'My substring1. My substring2: My substring3: My substring4'

Ideally, my aim is to extract 'My substring2' from this string with Python regex. However, I would also be pleased with a result that resembles '. My substring2:'

So far, I am able to extract

'. My substring2: My substring3:'

with

"\.\s.*:"

Alternatively, I have been able to extract - by using Wiktor Stribiżew's solution that deals with a somewhat similar problem posted in How can i extract words from a string before colon and excluding \n from them in python using regex -

'My substring1. My substring2'

specifically with

r'^[^:-][^:]*'

However, I have been unable, after many hours of searching and trying (I am quite new to regex), to combine the two results into a single effective regex expression that will extract 'My substring2' out of my aforementioned string.

I would be eternally greatfull if someone could help me find to correct regex expression to extract 'My substring2'. Thanks!

like image 552
Derk Avatar asked Jan 30 '26 05:01

Derk


2 Answers

You can use non-greedy regex (with ?):

import re

s = "My substring1. My substring2: My substring3: My substring4"

print(re.search(r"\.\s*(.*?):", s).group(1))

Prints:

My substring2
like image 196
Andrej Kesely Avatar answered Feb 01 '26 20:02

Andrej Kesely


You might for example exclude matching the dot as well, and use a capture group matching any char except the :

^[^:-][^:.]*\.\s*([^:]+)

Explanation

  • ^ Start of string
  • [^:-] The first char can not be either : or -
  • [^:.]* Optionally match any char except : or .
  • \.\s* Match a dot and optional whitespace chars
  • ([^:]+) Capture group 1, match 1+ chars other than :

Regex demo

Or a bit shorted if there can not be : . and - before matching the dot:

^[^:.-]+\.\s*([^:]+)

Regex demo | Python demo

For example

import re

s = "My substring1. My substring2: My substring3: My substring4"
pattern = r"[^:-][^:.]*\.\s*([^:]+)"
m = re.match(pattern, s)
if m:
    print(m.group(1))

Output

My substring2
like image 26
The fourth bird Avatar answered Feb 01 '26 20:02

The fourth bird