Dzmitry Dzmitry - 1 month ago 5
Python Question

regex greedy match ends with character or end of string

I have two strings:

1) "Created Account Owner Sharing Rule test1"

2) "Updated Account Owner Sharing Rule test3: Changed CaseAccessLevel from Read/Write to Read Only"

As result of regex matching I want to see:

1) Created Account Owner Sharing Rule test1

2) Updated Account Owner Sharing Rule test3

divided into 4 groups:

1) (Created) (Account) (Owner Sharing Rule) (test1)

2) (Updated) (Account) (Owner Sharing Rule) (test3)

For this purposes I tried use the next python regex:


but for the second string 4th group looks like 'test3: Changed CaseAccessLevel from Read/Write to Read Only'.

Essentially regex should stop either in the end of line or before ':' character but greedy match doesn't allow to do this.

Thanks in advance for the answers,


Jan Jan

You could do:

(?P<object_type>Owner\ Sharing\ Rule)\s
(?P<object_label>[^:\n]+)               # stop before : or newline

See a demo on (mind the different modifiers!).