Assistance creating a regex - regex

Can someone help me here? I need to create a regex that will meet the following criteria:
Valid characters: 0-9 and hyphen (-) only. Entry must be between 11 and 13 characters.
Also, must not contain the string "73480"
Thank you!

Use regex pattern ^(?!.*73480)[0-9-]{11,13}$

You want two regexes for clarity.
First check that this matches:
/^[-\d]{11,13}$/
and then check for failing to match:
/73480/
In Perl you'd do this like /^[-\d]{11,13}$/ && !/73480/. If this was PHP, you'd make two calls to preg_match.
Trying to cram it all into one regex makes things too hard to read in the future.

There's a great regex editor online which you could try: http://txt2re.com/.
Simply enter an example of what you'd like to match and then you can click to select patterns you would like to match on.

Related

Regex to match my requirement

Please help me with my requirement to match only specific pattern.
We have a string for example Jeorge Sally (34). my application should always pick the value that matches the pattern any name with (34).
I tried the following regex [A-z (34)]* but this is giving the different values also such as Adam siva (.
Thanks.
I believe the regex you are looking for is something like this:
^[A-z]+\s[A-z]+\s\(34\)$
This will match any first name, a space, a last name and then the 34 in brackets. Notice the brackets are escaped using a backslash.
I recommend using an online tool such as RegExr or Regex101 when you are trying to figure out where you are going wrong with your regular expressions as they provide visual aid to guide you.

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

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

Regular Expression, match ${ANY_TEXT}

Can you please help me with reg ex. I cant make it :( I hate regex.
I need to match this string ${ANY_TEXT} .Exactly one "${" and exactly one closing tag "}".
Thanks a lot. :)
\$\{[^}]+\} will match it. It will match ${ABC} from ${ABC}}} as well. If you want to match complete lines, simply anchor the regular expression using ^\$\{[^}]+\}$.
A good site to learn regular expressions is http://www.regular-expressions.info/.
I suppose this covers all the texts.
/^\$\{[a-zA-Z]\}$/

Regex match everything after question mark?

I have a feed in Yahoo Pipes and want to match everything after a question mark.
So far I've figured out how to match the question mark using..
\?
Now just to match everything that is after/follows the question mark.
\?(.*)
You want the content of the first capture group.
Try this:
\?(.*)
The parentheses are a capturing group that you can use to extract the part of the string you are interested in.
If the string can contain new lines you may have to use the "dot all" modifier to allow the dot to match the new line character. Whether or not you have to do this, and how to do this, depends on the language you are using. It appears that you forgot to mention the programming language you are using in your question.
Another alternative that you can use if your language supports fixed width lookbehind assertions is:
(?<=\?).*
With the positive lookbehind technique:
(?<=\?).*
(We're searching for a text preceded by a question mark here)
Input: derpderp?mystring blahbeh
Output: mystring blahbeh
Example
Basically the ?<= is a group construct, that requires the escaped question-mark, before any match can be made.
They perform really well, but not all implementations support them.
\?(.*)$
If you want to match all chars after "?" you can use a group to match any char, and you'd better use the "$" sign to indicate the end of line.
?(.*\n)+
With this you can get everything Even a new line
Check out this site: http://rubular.com/ Basically the site allows you to enter some example text (what you would be looking for on your site) and then as you build the regular expression it will highlight what is being matched in real time.
str.replace(/^.+?\"|^.|\".+/, '');
This is sometimes bad to use when you wanna select what else to remove between "" and you cannot use it more than twice in one string. All it does is select whatever is not in between "" and replace it with nothing.
Even for me it is a bit confusing, but ill try to explain it. ^.+? (not anything OPTIONAL) till first " then | Or/stop (still researching what it really means) till/at ^. has selected nothing until before the 2nd " using (| stop/at). And select all that comes after with .+.