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

Sign up to request clarification or add additional context in comments.

4 Comments

I wanted to get better with regexpressions.
Oh, I see, okay, give me a minute, I'll make edits and get back with regex answer.
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)
Get rid of the backslash in square brackets.
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 Comment

Thanks everyone for all of the help
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.