RegEx pickup words after specific word - regex

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!

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/

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("/")

Related

Improving a regex

I am looking for alternate methods to get john from the provided example.
My expression works as is but was hoping for some examples of better methods.
Example: john&home
my regexp: [a-z]{3,6}[^&home]
Im matching any character of length 3-6 upto but not including &home
Every item i run the regexp on is in the same format. 3-6 characters followed by &home
I have looked at other posts but was hoping for a reply specific to my regexp.
Most regex engines allow you to capture parts of a regex with capture groups. For instance:
^([A-Za-z]{3,6})&home$
The brackets here mean that you are interested in the part before the &home. The ^ and $ mean that you want to match the entire string. Without it, averylongname&homeofsomeone will be matched as well.
Since you use rubular, I assume you use the Ruby regex engine. In that case you can for instance use:
full = "john&home"
name = full.match(/^([A-Za-z]{3,6})&home$/).captures
And name will in this case contain john.

Regex: Search for verb roots

I've seen the results for classifying verbs by their endings. But I want to use Regular Expressions to find verb roots for regular verbs in Spanish.
I'm using this fancy site: http://regexpal.com/
Which I suspect may not be compatible with my end use, but will be a great starting point.
From what I have seen, the caret should identify all strings after it based on your supplied string-pattern.
So, to me:
ˆgust
Should find "gusta", "gustan", "gustamos", "gustas","gustar".
I know that I'm way off, but looking at many of the pages and tutorials and examples, I don't see anything that looks similar to what I want to do.
When you look for regex matching you'll get only the matching part, meaning, in case you have the word "gustan" and you're trying to match it with ^gust like you suggested, the output of the matcher will be "gust" - which is not what you want (you want the whole word).
So instead of matching to ^gust try matching to ^gust\w*$ which means anything that starts with "gust" and has zero or more characters following it.
^(gust[a-zA-Z]*)$
Edit live on Debuggex
^ denotes the start of the line
[a-zA-Z] letters only
* means zero or more
() is called a capture group
$ is the end of the line
If you want to edit with different words you could do this...
^((?:gust|otherwords)[a-zA-Z]*)$
Edit live on Debuggex
all you have to change/edit is |otherwords this will allow you to add more words that you want to match.
please read more about regex here and use debugexx.com to experiment.

How to evaluate a RegExp in an array with match groups?

I need to parse an array-like text with regular expression and get the match groups.
One example of then text I want to parse is this:
['red','green', 'blue']
I want to use match groups, because I want to extract them.
I am using this regular expression, but the groups found by it are not like what I expected:
\[ *('.+?')( *, *('.+?'))* *\]
The idea is to parse in this order:
A square bracket
Any number of spaces
A group with:
Single quote
Any character
Single quote
Zero or more groups of:
Any number of spaces
A comma
Any number of spaces
A group with
Single quote
Any character
Single quote
Any number of spaces
A square bracket
And get one group with each parsed array element.
Can you help me?
Hint: a easy way to test regexp is the site http://rubular.com
This isn't going to be a totalitarian answer, but I'm fairly certain you can't whitespace check by doing " *", at least it may depend on the language you're using.
Here's a C# regex example that shows some of the language requirements to check for whitespace: regex check for white space in middle of string
Edit: I see you added Ruby as your language, unfortunately I'm not verbose in Ruby so specifics I cannot help you with, sorry.
Edit2: Seeing as you're forcing yourself into Ruby to debug your regex statement, might I suggest: http://www.debuggex.com/ which tries to stay language independent?
Try this regex: '([^']+)', it should give you the following match groups red, green, blue according to rubular.com
You can match an arbitrary number of groups with one regex:
^\[\s*|(?:\G'([^']+)'\s*(?:,\s*|]$))+
or like this (should be more performant):
^\[\s*+|(?>\G'([^']++)'\s*+(?>,\s*+|]$))++
This work in ruby like asked before, in delphi I don't know.

What is wrong with my simple regex that accepts empty strings and apartment numbers?

So I wanted to limit a textbox which contains an apartment number which is optional.
Here is the regex in question:
([0-9]{1,4}[A-Z]?)|([A-Z])|(^$)
Simple enough eh?
I'm using these tools to test my regex:
Regex Analyzer
Regex Validator
Here are the expected results:
Valid
"1234A"
"Z"
"(Empty string)"
Invalid
"A1234"
"fhfdsahds527523832dvhsfdg"
Obviously if I'm here, the invalid ones are accepted by the regex. The goal of this regex is accept either 1 to 4 numbers with an optional letter, or a single letter or an empty string.
I just can't seem to figure out what's not working, I mean it is a simple enough regex we have here. I'm probably missing something as I'm not very good with regexes, but this syntax seems ok to my eyes. Hopefully someone here can point to my error.
Thanks for all help, it is greatly appreciated.
You need to use the ^ and $ anchors for your first two options as well. Also you can include the second option into the first one (which immediately matches the third variant as well):
^[0-9]{0,4}[A-Z]?$
Without the anchors your regular expression matches because it will just pick a single letter from anywhere within your string.
Depending on the language, you can also use a negative look ahead.
^[0-9]{0,4}[A-Za-z](?!.*[0-9])
Breakdown:
^[0-9]{0,4} = This look for any number 0 through 4 times at the beginning of the string
[A-Za-z] = This look for any characters (Both cases)
(?!.*[0-9]) = This will only allow the letters if there are no numbers anywhere after the letter.
I haven't quite figured out how to validate against a null character, but that might be easier done using tools from whatever language you are using. Something along this logic:
if String Doesn't equal $null Then check the Rexex
Something along those lines, just adjusted for however you would do it in your language.
I used RegEx Skinner to validate the answers.
Edit: Fixed error from comments

Capture followed by Digits: Replace Syntax? (Dreamweaver)

When you address a regex capture, things can get tricky when digits follow the capture. In PCRE, I can write
${1}000
to substitute the capture of Group 1 followed by three zeroes.
Does anyone know the equivalent syntax in Dreamweaver replace operations, if any?
If we had a series of "A"s instead of zeroes, we could use:
$1AAAA
But these:
$10000
${1}0000
do not work.
I believe the regex flavor is ECMAScript. Just cannot find the information.
This may not be addressed in the syntax. If so, that would be good to know.
Thank you!
Edit: I should add that this is not matter of life and death as I have a number of grep tools at my fingertips. I would just like to know.
Dreamweaver's regular expression find and replace is supposed to be based on JavaScript's implementation of RegExp. You should be able to just use $1000 in the replacement text. However, like you've found, the replacement groups ($ + group number) are not properly recognized when the replacement text has digits immediately after the grouping token.
FWIW: I've logged a bug on this at http://adobe.ly/DWwish