Reputation: 14902
What is the regular expression to get the position of the last separator character before a certain pattern?
For example, I have a string as follows
some value;some other value;some special XYZ value;some more special XYZ value
Here, the separator character is semicolon (";") and the pattern I am looking for is any string that contains "XYZ".
The correct answer is this case is position 28, which is the last occurence of the separator character (";") before an element containing the match pattern ("XYZ").
What would the regular expression look like?
Upvotes: 0
Views: 2602
Reputation: 5452
You can do it in two steps:
i.e.,
s = "some value;some other value;some special XYZ value;some more special XYZ value"
s1 = re.findall("^.*?;.*?XYZ",s)[0]
len(re.findall("(.*;)",s1)[0]) --> 28
Upvotes: 0
Reputation: 336468
;(?=[^;]*XYZ)
matches a semicolon that's followed by XYZ
within the same segment of the string.
Use it once to find the first occurrence. If you use it repeatedly, you'll find the following occurrences, too.
Upvotes: 1
Reputation: 32817
Use this regular expression
^.*(;).*XYZ.*$
.*(;).*XYZ.*
would match greedily i.e. it would match till the last occurance of ;
having XYZ
followed by it
Upvotes: 1