Jonathan O'Grady
Jonathan O'Grady

Reputation: 11

RegEx pickup words after specific word

Having a bit of the RegEx brain fart, and could really do with some kind assistance if anyone has time please?

I would like to pick up all words for URL after domain name.

For example:

http://www.bbc.co.uk/programmes/b08y26qp

Should return: programmes,b08y26qp

I have got this far:

[a-z][a-z0-9]*

But how do I qualify to begin returning words after http://www.bbc.co.uk/?

Very many thanks!

Upvotes: 0

Views: 325

Answers (2)

Tezra
Tezra

Reputation: 8833

You just need to prepend http://www.bbc.co.uk/ as string literal to your regex. You should also use the string start anchor (^) to reduce work on a failed match (^http:\/\/www\.bbc\.co\.uk\/)

Online

You can go to https://regex101.com/, and just add a \ before each (non grey) highlighted character until the whole regex only has grey highlights.

Java

In Java, just let Pattern.quote(string) and Matcher.quoteReplacement(string) do the escaping for you.

Of course, if you have a programming language, Something like this would be better. urlString.substring("http://www.bbc.co.uk/".length()+1).split("/")

Upvotes: 1

Aedvald Tseh
Aedvald Tseh

Reputation: 1917

Using $ you bind the regex to the end of the line. In this case it does matter what's in the begining.

Using () you can specify groups. This allows to retrieve results easily.

This regex applied to http://www.bbc.co.uk/programmes/b08y26qp

([A-Za-z0-9]+)\/([A-Za-z0-9]+)$

results in:

Group 1: programmes
Group 2: b08y26qp

See this example also in regex 101: https://regex101.com/r/YkUHk5/1/

Upvotes: 1

Related Questions