1

Here's what I'm trying to do: http://i.imgur.com/Xqrf8Wn.png

Simply take a URL with 3 groups, $1 not so important, $2 & $3 are but $2 is totally optional including (obviously) the corresponding backslash when present, which is all I am trying to make optional. I get that it can/should? be in a non-cap group, but does it HAVE to be? I've seen enough now seems to indicate it does not HAVE to be. If possible, I'd really like to have someone explain it so I can try to fully understand it, and not just get one possible working answer handed to me to simply copy, like some come here seeking.

Here's my regex string(s) tried and at best only currently matching second URL string with optional present:

^https:\/\/([a-z]{0,2})\.?blah\.com(?:\/)(.*)\/required\/B([A-Z0-9]{9}).*
^https:\/\/([a-z]{0,2})\.?blah\.com(\/)?(.*)\/required\/B([A-Z0-9]{9}).*
^https:\/\/([a-z]{0,2})\.?blah\.com(?:\/)?(.*)?\/required\/B([A-Z0-9]{9}).*

Here are the two URLs that I want to capture group 2 & 3, with 1 and 2 being optional, but $2 being the problem. I've tried all the strings above and have yet to get it to match the string when the optional is NOT present and I believe it must be due to the backslashes?

https://blah.com/required/B7BG0Z0GU1A
https://blah.com/optional/required/B7BG0Z0GU1A
Kobi
  • 125,267
  • 41
  • 244
  • 277
Collin Chaffin
  • 672
  • 7
  • 14
  • 1
    How about [`https?:\/\/([^/]+).*?\/required\/(.+)`](https://regex101.com/r/F3qVrO/1) – Jan Apr 23 '17 at 08:31
  • 1
    I removed a lot of the text because it distracts from the question, Stack Overflow isn't really a good place for rants :) . The question is missing expected output, so I may have gotten my answer wrong. Can you please add the expected output? What should be captured? What isn't valid? – Kobi Apr 23 '17 at 08:47
  • As I asked I really want more than just the string was hoping for an explanation and an answer with examples of how to make a portion of a regex optional, whether in non-cap group or not and your string is just as Japenese to me as the ones I came up with that didn't work. :) But thank you for the string! I'm confused why most of my question/explanation was removed actually if you read what you removed it was explaining why this has been asked 100 times with answers on S.O. and NONE actually answer and only provide a string but maybe 2/3rds of their questions were removed too, LOL! :) – Collin Chaffin Apr 23 '17 at 20:57

1 Answers1

1

Making a part of the pattern optional is as simple as adding ?, and your last two attempts both work: https://regex101.com/r/RIKvYY/1

Your mistake is that your test is wrong - you are using ^ which matches the beginning of the string. You need to add the /m flag (multiline) to make it match the beginning of each line. This is the reason your patterns never match the second line...

Note that you're allowing two slashes (//required, for example). You can solve it by joining the first slash and the optional part to the same capturing group (of course, as long as you are using .* you can still match multiple slashes):

https:\/\/([a-z]{0,2})\.?blah\.com(?:\/(.*))?\/required\/B([A-Z0-9]{9}).*
Community
  • 1
  • 1
Kobi
  • 125,267
  • 41
  • 244
  • 277
  • No, unless I'm missing something I'm running the regex (want to) in a loop multiple times against those strings, one at at time. But, your answer is correct and when I transferred to this question I apparently pasted the 3rd regex string that was in my list that I honestly must never have ran - since as you point out it IS correct. I never ran with the additional 2nd questionmark in between that was what was missing. So, for the part of my question (2/3rds actually) that the moderator removed, is the answer truly that a QUESTION MARK after a substring is optional whether or not in a group? – Collin Chaffin Apr 23 '17 at 21:02
  • EDIT: You are absolutely CORRECT and THAT is one thing I've been doing wrong but it's not a regex thing per say- since 100% of my regex's are single-line HOWEVER it's a SITE thing...the regex101 and way regex inherently works KEY when having to execute without the JS once against a list, then it is mutliline which is really I think what I never checked off when I tried all those strings because then, as you've correctly pointed out, the 2nd never matched. :) Thanks so much! And, in the event I do ever need to run a multiline regex, you've now taught me what I was missing! +1 – Collin Chaffin Apr 23 '17 at 21:06