Regex starting with certain set of characters - regex

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

Related

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

Regex : expression to match specific word

I have a regex replace function:
reg_replac​e(Input_Colu​mn,'\b(?:(?!https|www|http)\w)+\b', 'x')
With www.google.com input, the result is www.x.x where as it should be www.xxxxxx.xxx.
Please help me to write a regex which works by letters and not by words.
Use
\w(?!\w*\b(?<=\bhttps|\bwww|\bhttp))
See proof
Explanation
--------------------------------------------------------------------------------
\w word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\w* word characters (a-z, A-Z, 0-9, _) (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
https 'https'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
www 'www'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
http 'http'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
) end of look-ahead

Match if the line has two or more of the same capitalized word

Basically I want to match this:
So this. So that. [this should match]
Yes this. No that. [this shouldn't match]
I thought this would work:
(\b(\w+)\1\b.*){2,}
But right now, it's matching the second line too: https://regexr.com/5jhag
Why is this and how to fix it?
Match if the line has two or more of the same capitalized word
As you want to match capitalized words only a \w is not right because it matches [a-zA-Z0-9_] characters. Also using \1 just after the capture group means consecutive repeats only. Finally \b is also required around matches.
You may use this regex:
\b([A-Z]\w*)\b.*\b\1\b
RegEx Demo
RegEx Details:
\b: Word boundary
([A-Z]\w*): Match a capitalize word that start with uppercase letter followed by 0 or more of any word characters
\b: Word boundary
.*: Match 0 or more of any characters
\b\1\b: Match same word as what we captured in group #1 surrounded with word boundaries
(\b(\w+)\1\b.*){2,} is a repeated capturing group. \1 is a backreference that references the value of the group it is defined in and it is always assigned an empty string, at each iteration. Note: if you were to test with PCRE engine, there would be no match, see proof, because \1 is not empty, it is null and there is no match.
Your regex matches Yes this. No that. because the current expression is equal to (\b(\w+)\b.*){2,} and matches any word, then any text, two times or more.
Use
.*\b([A-Z][a-zA-Z]+)\b.*\b\1\b.*
See proof.
Unicode version:
.*\b(\p{Lu}\p{L}+)\b.*\b\1\b.*
See another proof.
Explanation
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
[a-zA-Z]+ any character of: 'a' to 'z', 'A' to 'Z'
(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
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most 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
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))

ReGex, How to find second instance of string

If I want to get the Name between “for” and “;” which is NISHER HOSE, can you help me find the correct regex expression as there is more than one "for’ and “;” in the string
Data Owner Approval Needed for Access Request #: 2137352 for NISHER HOSE; CONTRACTOR; Manager: MUILLER, TIM (TWM0069)
Using the regular expression (?<=for).*(?=;) I get the wrong match Access Request #: 2137352 for NISHER HOSE; CONTRACTOR - see screenshot on https://www.regextester.com/
Thanks
If you only want to assert for on the left, you should and make sure to not match for again and you should exclude matching a ; while asserting there is one at the right.
(?<=\bfor )(?:(?!\bfor\b)[^;])+(?=;)
Explanation
(?<=\bfor ) Assert for at the left
(?:(?!\bfor\b)[^;])! Match 1+ times any char except ; if from the current position not directly followed by for surrounded by word boundaries
(?=;) Assert ; directly at the right
Regex demo
Use
(?<=\bfor )(?![^;]*\bfor\b)[^;]+
See proof.
Explanation
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
for 'for '
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
[^;]* any character except: ';' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
for 'for'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[^;]+ any character except: ';' (1 or more times
(matching the most amount possible))
The main issue here is that there are two "for". If you want to catch the name then use the ":" as a delimiter to catch the second "for":
Regex: /:.*for(.+?);/gm
Demo: https://regex101.com/r/p3QY0o/1
The name will be captured in group 1. If you decide to use a lookahead/lookbehind just bear in mind that these may or may not be supported depending on the regex engine.