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.

3

4 Answers 4

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

4
  • I wanted to get better with regexpressions. Commented Jan 4, 2019 at 19:46
  • Oh, I see, okay, give me a minute, I'll make edits and get back with regex answer.
    – Amit Amola
    Commented Jan 4, 2019 at 19:47
  • Thanks. I know it has something to do with using <= but I tried placing that but I kept getting none when I print f1.group(0) or f1.group(1) Commented Jan 4, 2019 at 19:48
  • Get rid of the backslash in square brackets.
    – Jan
    Commented Jan 4, 2019 at 20:17
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).

1
  • Thanks everyone for all of the help Commented Jan 4, 2019 at 20:02
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

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.