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?

4
  • 1
    How did you test the last regex? See regex101.com/r/K3VRZo/1 Commented Dec 2, 2016 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! Commented Dec 2, 2016 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? Commented Dec 2, 2016 at 19:16
  • I'm voting to close this question as off-topic because it is about how to test regex patterns at regex101.com Commented Dec 2, 2016 at 19:28

1 Answer 1

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.

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

2 Comments

But when I run this (via 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?
The required groupings (the things inside the ()) are already there. Refer here 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). Note that these special match variables work in almost any language that supports regex matching.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.