0

I'm having a hard time matching multiple patterns in my regex and I'm wondering what could be wrong.

My test string: https://api.github.com/repos/baxterthehacker/public-repo/releases/1261438

These work as expected:

/https:\/\/api\.github\.com\/repos\/

Matches: https://api.github.com/repos/

/\/releases\/.*

Matches: /releases/1261438

My question is how do I combine the two into one statement? This is what I've tried but it only matches the first pattern and not the second for some reason.

/(https:\/\/api\.github\.com\/repos\/)|(\/releases\/.*)/

Matches: https://api.github.com/repos/

What am I doing wrong? Why is the second pattern ignored?

wise_gremlin
  • 443
  • 4
  • 15
  • 1
    How did you test the last regex? See https://regex101.com/r/K3VRZo/1 – Wiktor Stribiżew Dec 02 '16 at 17:58
  • It looks like adding the global flag does the trick. Now to figure out how to get ruby to play nice with it. :) Thanks! – wise_gremlin Dec 02 '16 at 18:56
  • So, the question is off=topic, since you have not run the regex in the target environment. How to test regexps at online testers is off-topic. Do you want to edit the question to show the real problem, or shall we close it? – Wiktor Stribiżew Dec 02 '16 at 19:16
  • I'm voting to close this question as off-topic because it is about how to test regex patterns at http://regex101.com – Wiktor Stribiżew Dec 02 '16 at 19:28

1 Answers1

1

The | is an OR operator for regex. It does not do concatenation. Try removing the | character from your regex string and see the magic.

The regex /(https:\/\/api\.github\.com\/repos\/).*(\/releases\/.*)/ does the trick.

Quirk
  • 1,305
  • 4
  • 15
  • 29
  • But when I run this (via https://regex101.com/), it matches the entire string. What I'm trying to do in Ruby is to match the first pattern and second pattern and strip those out, leaving what is left behind. What I'm looking for is "match this pattern AND this pattern". Is this possible? – wise_gremlin Dec 02 '16 at 18:53
  • The required groupings (the things inside the `()`) are already there. Refer [here](http://stackoverflow.com/a/9304049/284416) to learn how to access grouped sub-expression matches. Otherwise just use the special match variables **$1.. $n** to access those (More on this [here](http://stackoverflow.com/a/9304046/2844164)). Note that these special match variables work in almost any language that supports regex matching. – Quirk Dec 02 '16 at 18:59