0

Issue: I need my regex to have an optional group. Specifically "www" and "(https|http):.

Regex:

/\A^(https|http):\/\/www\.twitter\.com\/\w+\/status\/\d+/

Validation (FYI)

validates :twitter_link, format: { with: /\A^(https|http):\/\/www\.twitter\.com\/\w+\/status\/\d+/}

I need to make the "www" optional. Everything else seems to be working good.

What I need (and in order):

  • Might or might not start with "http:" or "https:".

  • Must include: "twitter.com/".

  • Must include: Any letter/number/character after "twitter.com/".

  • Must include: "status" after the twitter #{twitter_user_name_} like "/gem/status".

  • Must include: Only numbers after "status/"

Such as these possible links:

Possible Links:

urls = [
  "https://twitter.com/Twitt_erDev/status/850006245121695744",
  "http://twitter.com/Twit1243terDev/status/850006245121695744",
  "https://www.twitter.com/Twi234_tterDev/status/850006245121695744",
  "http://www.twitter.com/TwitterDev/status/850006245121695744",
  "http://m.twitter.com/Tw11itterDev/status/850006245121695744",
  "https://m.twitter.com/Tw11itterDev/status/850006245121695744",
  "www.twitter.com/Twitt11erDev/status/850006245121695744",
  "m.twitter.com/Tw11itterDev/status/850006245121695744",
  "twitter.com/Twitte345_rDev/status/850006245121695744",
]

How to make the "www" and "http/https" optional? And is my regex secure/good?

uno
  • 1,421
  • 12
  • 38
  • I fixed your first rule as you mentioned more than once that "http[s]:" is optional. If you think something is wrong with the edit, please feel free to rollback or edit the question further to clarify. – 41686d6564 stands w. Palestine Jan 14 '19 at 01:01

1 Answers1

1

To make something optional, you should use the ? quantifier, which basically means zero or one times.1 Now, if what you want to make optional is more than one character, you simply put it in a group (preferably, a non-capturing group) and then follow it with the question-mark-quantifier.

Something like the following should work for all your examples:

^(?:https?:\/\/)?(?:(?:www|m)\.)?twitter\.com\/\w+\/status\/\d+

Try it online.


References:


1 Another optional quantifier is the *, which means between zero and unlimitted times but it's not the right fit for your case.

  • Probably should put a $ at the end since it must end with the numbers as well. – Mark Jan 14 '19 at 01:19
  • @Mark Sure, if the URL always ends there. I decided to leave that up to the OP because they might have `/Something` after the numbers. – 41686d6564 stands w. Palestine Jan 14 '19 at 01:21
  • 1
    Considering that www.m.twitter is not a valid candidate, I would tweak that to `(?:(www|m)\.))?` – Happy Green Kid Naps Jan 14 '19 at 01:28
  • @Mark in order to use the "$" --- how would i tack that on because I would like to use that as the number sequence will be the last group of the url validation. ive tried a few ways but not working. I tried just putting d+$/ but that won't work for some reason. I also realized i need an optional hash "/" for the absolute end of the URL in case someone includes it – uno Jan 14 '19 at 07:02
  • For instance (this works in the website tester) i did: /\A^(?:https?:\/\/)?(?:(?:www|m)\.)?twitter\.com\/\w+\/status\/\d+(?:\/\/?)?$\// --- as a validator but it won't work for some reason... – uno Jan 14 '19 at 07:12
  • Just add to the end of @Ahmed's regex: /\A^(?:https?:\/\/)?(?:(?:www|m)\.)?twitter\.com\/\w+\/status\/\d+$/ – Mark Jan 14 '19 at 07:19
  • Looks like the issue was using the $. I just replaced it with \z --- I guess ruby/rails doesn't like using that as it's apparently a security issue. That'swhat the error said at least . thanks tho! everything seems to be working fine! – uno Jan 15 '19 at 01:55