# Regular expression that if it matches the first group ignores the second group?

**URL:** <https://forum.golangbridge.org/t/regular-expression-that-if-it-matches-the-first-group-ignores-the-second-group/32776>\
**Category:** Getting Help\
**Created:** [August 22, 2023, 3:11am UTC](https://forum.golangbridge.org/t/regular-expression-that-if-it-matches-the-first-group-ignores-the-second-group/32776 "2023-08-22T03:11:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willy](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/willy/32/11447_2.png) [@Willy](https://forum.golangbridge.org/u/Willy)\
**Post date:** [August 22, 2023, 3:11am UTC](https://forum.golangbridge.org/t/regular-expression-that-if-it-matches-the-first-group-ignores-the-second-group/32776/1 "2023-08-22T03:11:06Z")

</div>

I have this regular expression:

```auto
isValidPath = regexp.MustCompile(`/user/action/?(.*)`)

```

It matches these 2 strings that are of interest to me:

```auto
"/user/action" and "/user/action/someResource"

```

But it also matches this other one:

```auto
"/user/actionblablablabla"

```

How do I make **isValidPath** not match this last one? It should only match these two paths:

```auto
"/user/action" and "/user/action/someResource"

```

Where **someResource** can be anything.

---

<div class="post-metadata">

**Author:** ![Willy](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/willy/32/11447_2.png) [@Willy](https://forum.golangbridge.org/u/Willy)\
**Post date:** [August 22, 2023, 4:09am UTC](https://forum.golangbridge.org/t/regular-expression-that-if-it-matches-the-first-group-ignores-the-second-group/32776/2 "2023-08-22T04:09:01Z")

</div>

This works:

```auto
isValidPath = regexp.MustCompile(`/user/action$|/user/action/(.*)`)

```

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [August 22, 2023, 10:34am UTC](https://forum.golangbridge.org/t/regular-expression-that-if-it-matches-the-first-group-ignores-the-second-group/32776/3 "2023-08-22T10:34:06Z")

</div>

`/user/action(/(.*))?` and use match group 2 rather than 1 to extract the ressource.

This would still allow for `/user/action/`, if thats not wanted use `.+` for the inner group instead.

Anyway, I do not thing that using a regex to validate this is a good thing, instead you probably want to use [`filepath.Match`](https://pkg.go.dev/path/filepath#Match), as this generally also makes sure that the path is valid.

Your current regex (as well as my suggestion) would allow `someRessource/someSubRessource`, which might or might not be what you want. With the help of the mentionen function and those globs you could make it more explicit. You could as well with a regexp, but that again would make the regex more and more complicated.

Last but not least, I’d probably use 2 seperate checks rather than one combined for equialtiy to the base path and being some resource path.

---

<div class="post-metadata">

**Author:** ![Willy](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/willy/32/11447_2.png) [@Willy](https://forum.golangbridge.org/u/Willy)\
**Post date:** [August 23, 2023, 8:13am UTC](https://forum.golangbridge.org/t/regular-expression-that-if-it-matches-the-first-group-ignores-the-second-group/32776/4 "2023-08-23T08:13:33Z")

</div>

@NobbZ

**someResource** can be anything, all I want is a true or false, with your proposal I have to extract the path and then re-verify and that is not what I am looking for, I just want to know if the path meets that criteria and then pass the control to another package that will take care of the rest, I only need 1 regular expression for a particular case and it is not to validate thousands of paths as a router would do, so:

```auto
package main

import (
	"fmt"
	"regexp"
)

var rNum = regexp.MustCompile(`/user/action/(.*)|/user/action$`)

func main() {
	group := []string{
		"/user/action/",
		"/user/action/someresource/cat.png",
		"/user/action",
		"/user/actionblabla",
	}
	for _, urlPath := range group {
		fmt.Println(fmt.Sprintf("%s -> %t", urlPath, rNum.MatchString(urlPath)))
	}
}

```

With my regular expression:  
`var rNum = regexp.MustCompile(`/user/action/(.*)|/user/action$`)`

Output:

```auto
/user/action/ -> true
/user/action/someresource/cat.png -> true
/user/action -> true
/user/actionblabla -> false

```

For now that is my solution.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [November 21, 2023, 8:14am UTC](https://forum.golangbridge.org/t/regular-expression-that-if-it-matches-the-first-group-ignores-the-second-group/32776/5 "2023-11-21T08:14:03Z")

</div>

Closed on November 21, 2023, 8:14am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
