M
M
Mikola Poltavtsev2020-06-09 10:29:29
Regular Expressions
Mikola Poltavtsev, 2020-06-09 10:29:29

How to regex indicate end of line or question mark?

I'm trying to parse a folder ID in a Google Drive link.
Link Example

https://drive.google.com/drive/folders/1q5nL2z8yKS-0qvmlt9Jtq8u3iFgsu-ea

Also, the link can have a parameter at the end. Like this:
https://drive.google.com/drive/folders/1q5nL2z8yKS-0qvmlt9Jtq8u3iFgsu-ea?usp=sharing

The task is to isolate the ID, in the example above it is 1q5nL2z8yKS-0qvmlt9Jtq8u3iFgsu-ea
Now the regular expression looks like this:
^(?:https?:\/\/)?(?:www\.)?(?:drive\.google\.com\/drive\/folders\/)(.+)$

And it does not take into account that the desired part can end either with a question mark or the end of the line.
Tried adding something like (\?|\$) - doesn't work.
What to feed him there?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2020-06-09
@Poltavtcev

Tried adding something like (\?|\$) - doesn't work.
What to feed him there?

There's an extra slash before the $.
(.+?)(\?|$)

D
Dmitry Derepko, 2020-06-09
@xEpozZ

^(?:https?:\/\/)?(?:www\.)?(?:drive\.google\.com\/drive\/folders\/)(.+)[\?\$]

https://regex101.com/r/WaVWMZ/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question