0

I would like identify the string of characters that is between two patterns (lettre/ and " in example). In addition, the identified string should not correspond to a third pattern (somth?other in example).

Python 3.7 running on MAC OSX 10.13

import re
strings = ['lettre/abc"','lettre/somth?other"','lettre/acc"','lettre/edf"de','lettre/nhy"','lettre/somth?other"']

res0_1 = re.search('lettre/.*?\"', strings[0])
res1_1 = re.search('lettre/.*?\"', strings[1])
print(res0_1)
<re.Match object; span=(0, 11), match='lettre/abc"'>
print(res1_1)
<re.Match object; span=(0, 19), match='lettre/somth?other"'>

res0_2 = re.search('lettre/(.*?\"&^[somth\?other])', strings[0])
res1_2 = re.search('lettre/(.*?\"&^[somth\?other])', strings[1])
print(res0_2)
None
print(res1_2)
None

I would like to get res0_1 for strings[0] and res1_2 for strings[1].

Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43
Charlie
  • 49
  • 3
  • 2
    What do you mean to say? That you do not want to get a match if there is `somth?other` to the right of `/`? Try `r'lettre/(?!somth\?other)[^"]*"'`. See [the regex demo](https://regex101.com/r/KepctJ/1). – Wiktor Stribiżew Aug 09 '19 at 14:02
  • This may be related: https://stackoverflow.com/questions/1240275/how-to-negate-specific-word-in-regex – GZ0 Aug 09 '19 at 14:06
  • @WiktorStribiżew That's exactly what I need, thanks. – Charlie Aug 09 '19 at 14:40

4 Answers4

0

As I understand it Try this:

import re
strings = ['lettre/abc"','lettre/somth?other"','lettre/acc"','lettre/edf"de','lettre/nhy"','lettre/somth?other"']

res0_1 = re.findall('lettre/(.*)\"', strings[0])
res1_2 = re.findall('lettre/(.*)\"', strings[1])
print(res0_1)
print(res1_2)

Hope it helps

0

I think below code can give you what you asked in the question.

import re
strings = ['lettre/abc"','lettre/somth?other"','lettre/acc"','lettre/edf"de','lettre/nhy"','lettre/somth?other"']

for i in strings:
    if 'somth?other' not in i.split('/')[1]:
        print(i.split('/')[1].split('"')[0])
Rangeesh
  • 361
  • 1
  • 13
0

Since you do not want to get a match if there is somth?other to the right of / you may use

r'lettre/(?!somth\?other)[^"]*"'

See the regex demo and the regex graph:

enter image description here

Details

  • lettre/ - a literal substring
  • (?!somth\?other) - no somth?other substring allowed immediately to the right of the current location
  • [^"]* - 0+ chars other than "
  • " - a double quotation mark.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
-1

Try to use this site instead of try and error. https://regex101.com/

In [7]: import re 
   ...: strings = ['lettre/abc"','lettre/somth?other"','lett
   ...: re/acc"','lettre/edf"de','lettre/nhy"','lettre/somth
   ...: ?other"'] 
   ...:                                                     

In [8]: c = re.compile('(?=lettre/.*?\")(^((?!.*somth\?other
   ...: .*).)*$)')                                          

In [9]: for string in strings: 
   ...:     print(c.match(string)) 
   ...:                                                     
<re.Match object; span=(0, 11), match='lettre/abc"'>
None
<re.Match object; span=(0, 11), match='lettre/acc"'>
<re.Match object; span=(0, 13), match='lettre/edf"de'>
<re.Match object; span=(0, 11), match='lettre/nhy"'>
None