Beppe94
Beppe94

Reputation: 15

Regex to extract string if there is or not a specific word

Hi I'm a regex noob and I'd like to make a regex in order to extract the penultimate string from the URL if the word "xxxx" is contained or the last string if the word "xxxx" is not contained.

For example, I could have 2 scenarios:

In both cases I want to extract the string 1adf0023efae456.

I've tried something like (?=(\w*xxxx\w*)\/.*\/(.*?)\/|[^\/]+$) but doesn't work properly.

Upvotes: 0

Views: 199

Answers (2)

The fourth bird
The fourth bird

Reputation: 163362

You can match the forward slash before the digits, then match digits and assert what follows is either xxxx or the end of the string.

\d+(?=/xxxx|$)

Regex demo

If there should be a / before matching the digits, you could use a capturing group and get the value from group 1

/(\d+)(?=/xxxx|$)
  • / Match /
  • (\d+) Capture group 1, match 1+ digits
  • (?=/xxxx|$) Positive lookahead, assert what is on the right is either xxxx or end of string

Regex demo

Edit

If there could possibly also be alphanumeric characters instead of digits, you could use a character class [a-z0-9]+ with an optional non capturing group.

/([a-z0-9]+)(?:/xxxx)?$

Regex demo

To match any char except a whitespace char or a forward slash, use [^\s/]+


Using lookarounds, you could assert a / on the left, match 1+ alphanumerics and assert what is at the right is either /xxxx or the end of the string which did not end with /xxxx

(?<=/)[a-z0-9]+(?=/xxxx$|$(?<!/xxxx))

Regex demo

Upvotes: 1

JohnyL
JohnyL

Reputation: 7142

You could avoid Regex:

string[] strings =
{
    "www.hello.com/aaaa/1adf0023efae456",
    "www.hello.com/aaaa/1adf0023efae456/xxxx"
};
var x = strings.Select(s => s.Split('/'))
        .Select(arr => new { upper = arr.GetUpperBound(0), arr })
        .Select(z => z.arr[z.upper] == "xxxx" ? z.arr[z.upper - 1] : z.arr[z.upper]);

Upvotes: 0

Related Questions