Regular Expression needed for a Specific ID - regex

I need to create a regular expression that matches an ID that has a specific format. The ID always begins with "OR" followed by 4 digits, then a dash, then another number that can be of any length. Examples of valid matches are:
OR1581-2
OR0057-101
OR0000-5312
OR3450-17371
Thanks!

Try ^OR\d{4}-\d+$.
The ^ matches the beginning of the string or line.
OR is not a special sequence and will match only those two characters in order.
\d matches any digit, and {4} is shorthand for listing the preceding group (the digit) exactly four times.
- is not a special character and will match only the hyphen.
\d matches any digit again, and the + requires the preceding group (the digit) to occur one or more times.
$ matches the end of the string or line.

If you need to find match in string that contains such ID, but also other text, then use
\bOR\d{4}-\d+\b
However if you need to verify input if is in such format, so no other text around is allowed, then go with
^OR\d{4}-\d+$

Related

Optional thousand-separator processes incomplete string

I need to process numbers that may have optional thousand-separators, such as 1234567 and 1,234,567
I naively assumed I could achieve this with
(\d{1,3}([,]?(\d{3}))*)
This, however, matches only 123456 (not the 7) and 1,234,567 (correctly)
However, if I specify an explicit number of matches (2 in this case)
(\d{1,3}([,]?(\d{3})){2})
or a bound (such as \b)
(\d{1,3}([,]?(\d{3}))*)\b
the full match is performed.
Why does the “greedy” * quantifier stop after the first match in the first regex?
If you want to match both numbers with, and without, proper comma thousands separators, then I would use an alternation:
^(\d{1,3}(?:,\d{3})*|\d+)$
Demo
The reason is that \d{1,3} is greedy, so it matches 123 at the beginning of the number. Then the rest of the regexp will only match groups of exactly 3 digits because it uses \d{3}. A regular expression doesn't try to match the longest possible string, so it won't backtrack and shorten the match for \d{1,3} to make the rest of the regexp go further.
But if you add a word boundary \b at the end, it no longer matches with that 3-digit prefix. That causes it to backtrack until it's able to match groups of 3 digits ending with a word boundary.

Regex pattern for first name without whitespace or numbers

I have this regex pattern
/^[^-\s][^0-9][a-zA-Z\s-]+$/
I am a bit confused on why when I test it on https://www.regextester.com/
My pattern allows one single number to be added before the string. Meaning that if I type in '2Mantas' it will still accept it whereas '22Mantas' will fail the test. I do not want any numbers or whitespace to be allowed. Any ideas anyone?
You have two negation groups so it is saying the first character cannot be whitespace and the second character cannot be a number. If you put the whitespace and digit in the first brackets it will work as desired.
^[^-\s\d][a-zA-Z\s-]+$
The first two rules in your current regular expression break down to the following:
^[^\s-] - the first character in the string should not be a whitespace or a hyphen. This explains why 2steve is accepted - 2 is not a whitespace or a hyphen character.
[^0-9] - the second character in the strnig should not be a digit. This iexplains why 22steve is not accepted - the 2 in the second position is a digit, which violates this rule.
Assuming you don't want anything but capital and lowercase letters in your first name input, and the name shouldn't start with a whitespace or hyphen character, you can simplify to a subset of your current regular expression:
/^[A-Za-z][A-Za-z-\s]+$/
Regex101
This should work
Get string, that starts with exactly one digit, and after this digits should be expression, that contains only strings (greedy)
^\d{1}([a-zA-Z]+)
https://regex101.com/r/wtBwd7/1

What is the diffrence between these three regular expressions

What is the main difference between the following 3 regular expressions.
1) /^[^0-9]+$/
2)/[^0-9]+/
3) m/[^0-9]+/
I am really trying to understand this, since researching online has not helped me much I was hoping I could find some help here.
All of them have [^0-9]+, which is one or more characters that are not the numbers 0, 1, ... to 9.
The first one /^[^0-9]+$/ is anchored at the start and end of the string, so it will match any string that only contains non-digits.
The second one /[^0-9]+/ is not anchored, so it matches any string that contains at least one (or more) non-digits.
The third one m/[^0-9]+/ is the same as the second, but uses the m// match operator explicitly.
For a good explanation, check out regex101.com for the first and second regex.
There's a difference between a regular expression and the match operator which takes a regular expression as its operand.
You only have two regular expressions there - ^[^0-9]+$ and [^0-9]+. Option 3 uses the same regex as option 2, but it uses a different version of the match operator.
The difference between 1 and 2 is that 1 is anchored at the start and the end of the string, whereas 2 isn't anchored at all.
So 1 says "match the start of the string, followed by one or more non-digits, followed by the end of the string". 2 says "match one or more non-digits anywhere in the string".
Does that help at all?
The pattern [^0-9] is common to these three regexes, and will match any single character that is not a decimal digit
/^[^0-9]+$/
This anchors the pattern to the beginning and end of the string, and insists that it contains one or more non-digit characters
The circumflex ^ is a zero-width anchor that matches the beginning of the string
The dollar sign $ is also a zero-width anchor that will match either at the end of the string, or before a newline character if that newline is the last in the string. So this will match "aaa" and "aaa\n" but not "aa7bb\n"
/[^0-9]+/
This has no anchors, and so will return true if the string contains at least one non-digit character anywhere
It will match "12x345" and fail to match "12345". Note that a trailing newline counts as a non-digit character, so this pattern will match "123\n"
m/[^0-9]+/
This is identical to #2, but with the m placed explicitly. This is unnecessary if you are using the default slashes for delimiters, but it can be convenient to use something different if you are matching a pattern for, say, a file path, which itself contains slashes
Using m lets you choose your own delimiter, for example m{/my/path} instead of /\/my\/path/
In essence, #1 is asking whether the string is wholly composed of non-digit characters, while #2 and #3 are identical, and test whether the string contains at least one non-digit character

Regular expression let periods in (.)

My regular expression lets in periods for some reason, how can I keep that from happening.
Rules:
4-15 characters
Any alphanumeric characters
Underscore as long as it's not first or last
[A-Za-z][A-Za-z0-9_]{3,14}
I don't want "bad.example" for work.
Edit: changed to 4-15 characters
Your regex matches example as a substring of bad.example. Use anchors to prevent that:
^[A-Za-z][A-Za-z0-9_]{1,12}[A-Za-z]$
Note that (like your regex) this regex also prevents digits from matching in the first and last position - if they should be allowed (as per your specs), just add 0-9 at the end of the character classes.
^[A-Za-z][A-Za-z0-9_]{3,14}$
try this
This will match any alphanumeric at the beginning and end. In the middle it will accept from one up to twelve alphanumerics including an underscore:
^[a-zA-Z\d]\w{1,12}[a-zA-Z\d]$
It does not match bad.example but matches only example as your regex allows a character from 4 to 15.See here.
http://regex101.com/r/xV4eL5/5
To prevent it you need to match the whole input and not make partial matches.Put a ^ start anchor and $ end anchor.
Use
\A[A-Za-z0-9][\w]{1,12}[A-Za-z0-9]\Z

Regex to match [integer][colon][alphanum][colon][integer]

I am attempting to match a string formatted as [integer][colon][alphanum][colon][integer]. For example, 42100:ZBA01:20. I need to split these by colon...
I'd like to learn regex, so if you could, tell me what I'm doing wrong:
This is what I've been able to come up with...
^(\d):([A-Za-z0-9_]):(\d)+$
^(\d+)$
^[a-zA-Z0-9_](:)+$
^(:)(\d+)$
At first I tried matching parts of the string, these matching the entire string. As you can tell, I'm not very familiar with regular expressions.
EDIT: The regex is for input into a desktop application. I'm was not certain what 'language' or 'type' of regex to use, so I assumed .NET .
I need to be able to identify each of those grouped characters, split by colon. So Group #1 should be the first integer, Group #2 should be the alphanumeric group, Group #3 should be an integer (ranging 1-4).
Thank you in advance,
Darius
I assume the semicolons (;) are meant to be colons (:)? All right, a bit of the basics.
^ matches the beginning of the input. That is, the regular expression will only match if it finds a match at the start of the input.
Similarly, $ matches the end of the input.
^(\d+)$ will match a string consisting only of one or more numbers. This is because the match needs to start at the beginning of the input and stop at the end of the input. In other words, the whole input needs to match (not just a part of it). The + denotes one or more matches.
With this knowledge, you'll notice that ^(\d):([A-Za-z0-9_]):(\d)+$ was actually very close to being right. This expression indicates that the whole input needs to match:
one digit;
a colon;
one word character (or an alphanumeric character as you call it);
a colon;
one or more digits.
The problem is clearly in 1 and 3. You need to add a + quantifier there to match one or more times instead of just once. Also, you want to place these quantifiers inside the capturing groups in order to get the multiple matches inside one capturing group as opposed to receiving multiple capturing groups containing single matches.
^(\d+):([A-Za-z0-9_]+):(\d+)$
You need to use quantifiers
^(\d+):([A-Za-z0-9_]+):(\d+)$
^ ^ ^
+ is quantifier that matches preceeding pattern 1 to many times
Now you can access the values by accessing the particular groups