0

I am writing a regex to find a ? and capture everything before that. Here's what I have so far.

f = 'file.doc?gs_f'
f1 = re.search(r'(\?)',f)
print(f[:f1.start()]) #prints "file.doc"

How do I use the regex to capture everything before the ?. The is something called a look behind but I am unsure how to use it.

yash tyagi
  • 15
  • 1
  • 5
user3525290
  • 1,557
  • 2
  • 20
  • 47
  • Possible duplicate of [Extract string with Python re.match](https://stackoverflow.com/questions/13423624/extract-string-with-python-re-match) – Corion Jan 04 '19 at 19:39
  • Possible duplicate of [How would I get everything before a : in a string Python](https://stackoverflow.com/questions/27387415/how-would-i-get-everything-before-a-in-a-string-python) – Dani Mesejo Jan 04 '19 at 19:40
  • ... also https://stackoverflow.com/questions/11171045/python-regular-expression-example – Corion Jan 04 '19 at 19:43

4 Answers4

1

This would work:

^[^\?]+

And use findall:

f = 'file.doc?gs_f'
f1 = re.findall("^[^\?]+",f)

You can see a regex working below for any kind of string: Regex_Working

Amit Amola
  • 2,301
  • 2
  • 22
  • 37
1

Multiple possibilities:

  1. Either everything not a question mark: [^?]+
  2. Or a lazy quantifier with capturing groups: (.+?)\?
  3. Or a positive lookahead: .+?(?=\?)

See the corresponding demos on regex101.com (1, 2 and 3).

Jan
  • 42,290
  • 8
  • 54
  • 79
1

You do not need look behind.

f = 'file.doc?gs_f'
re.search(r'([\w\.]*(?=\?))',f).group(0)

A good here Regex lookahead, lookbehind and atomic groups

mad_
  • 8,121
  • 2
  • 25
  • 40
1

Yes, it is called Positive Look Ahead. You have to look ahead till ? comes. Here is the Regex which is performing your reqt. let me know if you have any question.

Regex: '.*(?=\?)'

Here,
.* will pull everything.
(?=) is a positive look ahead
\ escape character for question mark symbol. As question mark symbol is Quantifier in Regex.

Deep
  • 342
  • 3
  • 12