0

I have strings that could look like this:

http://mywebsite.com/1234
http://mywebsite.com/foo
http://mywebsite.com/bar
http://google.com

I want to use a regex to only grab the strings that are preceeded with "http://mywebsite.com/" and only contain letters after that (not numbers). So in my example, the following strings would be valid:

http://mywebsite.com/foo
http://mywebsite.com/bar

so far I have a regex that looks like this:

"http://mywebsite.com/[a-zA-Z]+"

but I'm not getting any results

DannyD
  • 2,732
  • 16
  • 51
  • 73
  • 1
    Please tag the language you're using – GalAbra Jun 05 '18 at 21:25
  • You probably need to escape the slashes: use `\/` instead of `/` – GalAbra Jun 05 '18 at 21:27
  • In addition to @GalAbra's comments, depending on what you mean by "grab", you might need a capture group, using `(` and `)`, in the regex; where to put them of course depends on how much of the desired strings you want to "grab" – landru27 Jun 05 '18 at 21:32
  • What tool/language are you using? JavaScript? Then see [this post](https://stackoverflow.com/questions/1707299/how-to-extract-a-string-using-javascript-regex). – Wiktor Stribiżew Jun 05 '18 at 21:43

1 Answers1

1

You just need to escape the slashes and dot using backslashes, but you should also be using $ at the end of your regex to make sure that there are no numbers (or other disallowed characters) following the match:

http:\/\/mywebsite\.com\/[a-zA-Z]+$

This does presume that you have each string occupying its own line in the input, or that there is not any other information after each string you are testing.

Try it out here


To "grab" the string after the final slash, use a capturing group:

http:\/\/mywebsite\.com\/([a-zA-Z]+)$

Try it out here

Then using your programming language, you can access the value stored in that group for each match. Or, if your regex flavour supports the \K sequence you could use:

http:\/\/mywebsite\.com\/\K[a-zA-Z]+$

to avoid the capturing group altogether.

Try it out here

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49