How can I use a regular expression to accomplish the following? - regex

The department number is either 5 digits, first digit has to be non-zero, or six digits, with a period and a 0 or 1 following.
So, first example would be 10000, 50050, 99999.
Second, 992000.1, 950000.0.
This is a simple "mask", so no coding is available in the GUI / native format.
Currently, I had [1-9]{1}[0-9]{0,4} for the first option

Use
\b(?:[1-9][0-9]{4}|[0-9]{6}\.[01])\b
See proof.
Explanation
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
[0-9]{4} any character of: '0' to '9' (4 times)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[0-9]{6} any character of: '0' to '9' (6 times)
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
[01] any character of: '0', '1'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char

Related

How to detect only combination of numbers and letters by regex

I need to detect only the word from a sentence where only combination of the numbers and letters exists by regex.
I am using this https://regex101.com/r/eSlu2I/1 ^[a-zA-Z0-9]* regex.
Here last two ones should be excluded.
Can anyone help me with this?
Use
^(?![a-zA-Z]+\b)[a-zA-Z0-9]*
See regex proof.
EXPLANATION
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
[a-zA-Z]+ any character of: 'a' to 'z', 'A' to 'Z'
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-zA-Z0-9]* any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (0 or more times (matching the
most amount possible))
You can use the following regex:
\w*\d\w*
Explanation:
\w*: optional combination of alphanumeric characters
\d: digit
\w*: optional combination of alphanumeric characters
Try it here.
EDIT: In case you require the presence of at least one letter together with the number, you can instead use the following regex:
\w*(\d[A-Za-z]|[A-Za-z]\d)\w*
Explanation:
\w*: optional combination of alphanumeric characters
(\d[A-Za-z]|[A-Za-z]\d):
\d[A-Za-z]|: digit + alphabetical character or
[A-Za-z]\d: alphabetical character + digit
\w*: optional combination of alphanumeric characters

Regex starting with certain set of characters

I have a requirement where the regex has to contains only certain set of characters .
For example requirement is that string can start with
JIRA-<5 digit number> or PROJ-<5 digit number>
This means allowed values can be as:
JIRA-12345
PROJ-98765
I tried regex as
(\JIRA-[0-9]+)|(\ PROJ-[0-9]+)
This seems to be not working, please suggest on how to proceed on this.
Thanks
Use
\b(?:JIRA|PROJ)-\d{5}\b
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
JIRA 'JIRA'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
PROJ 'PROJ'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
\d{5} digits (0-9) (5 times)
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char

Regex to find numbers in text that are encountered 3 or more times

I need an regex to find numbers that were encountered 3 or more times in text.
some text 577
some 123 text
577 some text
some 577 text
some text 512
I need regex to match 577
My last try was: (?:\d+){3,}
Use
\b([0-9]+)\b(?=(?:[\w\W]*?\b\1\b){2})
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
(?: group, but do not capture (2 times):
--------------------------------------------------------------------------------
[\w\W]*? any character of: word characters (a-
z, A-Z, 0-9, _), non-word characters
(all but a-z, A-Z, 0-9, _) (0 or more
times (matching the least amount
possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
){2} end of grouping
--------------------------------------------------------------------------------
) end of look-ahead

Regex to find 5 integers before last underscore of filename

I need to find 5 integers before the last underscore in a given filename.
Example string:
X130874_W907025343_Txt.pdf
I need to find 25353
The closest I came was (?<=_)[^_]+(?=[^_](.{5})_)
Use a lookahead after 5 digits that matches an underscore followed by no undercores until the end.
\d{5}(?=_[^_]*$)
Use
[0-9]{5}(?=_(?!.*_))
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
[0-9]{5} any character of: '0' to '9' (5 times)
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
_ '_'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
_ '_'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
) end of look-ahead

Explanation for a Regex

Can anyone please explain me what this regex signifies?
/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
I think this regex will validate the IPV4 address
Please use this link to get explation of your regex
Regex Explanation
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
2 '2'
--------------------------------------------------------------------------------
[0-4] any character of: '0' to '4'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
25 '25'
--------------------------------------------------------------------------------
[0-5] any character of: '0' to '5'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
( group and capture to \2 (3 times):
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
2 '2'
--------------------------------------------------------------------------------
[0-4] any character of: '0' to '4'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
25 '25'
--------------------------------------------------------------------------------
[0-5] any character of: '0' to '5'
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
){3} end of \2 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \2)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
While the answer above explains the regex atom by atom, I think the answer that you're looking for is "it matches IPv4 addresses."
To wit:
# Match the beginning of a string
/^
# Match a number from 1-255
([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
# Same as above with a . in front of it
(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))
# Match the above three times.
{3}
# Match end of the string
$/
It is look like an expression which accepts values from 1.0.0.0 to 255.255.255.255
A better explanation :
Meaning