regex use in Websphere MQ - websphere-mq-fte

As I am new to regex, can anyone give me the detailed explanation of the following regex:-
ZBC_EXTR[0-9]{0,1}\_STOCK_([A-Z]{0,1}|[0-9]{0,1})

This is regex is looking for a string with various parts to it.
"ZBC_EXTR" - The string must begin with these characters
"[0-9]{0,1}" - Looks for a digit 0-9, can appear 0 times but not more than once
"\" - Escape character(Escapes the following character for regex engine)
"_STOCK_" - Must have these set of characters next
"(" - Beginning of the group expression
"[A-Z]{0,1}" - Look for alpha, capital character,can appear 0 times but not more than once
"|" - The logic OR expression
"[0-9]{0,1}" - Looks for a digit 0-9, can appear 0 times but not more than once
")" - End of the group expression
For example the following strings would match the regex expression:
ZBC_EXTR8_STOCK_
ZBC_EXTR_STOCK_
ZBC_EXTR_STOCK_F
But these strings would NOT match:
ZBC_EXTR_STOCKF
ZBC_EXTRA_STOCK_F
ZBC_EXTR45_STOCK
A good resource for regex: https://www.regular-expressions.info/

Related

Regex to identify for values other than alphanumeric values which can have hyphen or dot in between them but not at beginning or at end

I am new to the regular expressions. I have seen other quite close posts with a similar question but as you are aware in RegEx even dot matters a lot so here I am posting this question to seek help in this particular scenario.
My SQL column value can have a-z, A-Z, and 0-9
It can have a dot(.) and hyphen(-) in between. These 2 things cannot be at the beginning or at the end.
It cannot have space or tabs or any blanks anywhere in the column value.
It cannot start or end with any special characters; not even dots or hyphens.
I wrote this query which covers the 1st, 2nd, and 3rd points but fails in the 4th case.
select * from test_db.xtmp_testtable_invalidchars042321_rg where (sl_id REGEXP '[^[:alnum:]].+$')
**Table column input values**
RaghavGupta
.RaghavGupta
#Raghav.Gupta
"Raghav Gupta"
Raghav Gupta
Raghav#Gupta
Raghav$Gupta
Raghav%Gupta
Raghav*Gupta
Raghav.Gupta
RaghavGupta
RaghavGupta$
RaghavGupta.
RaghavGupta[]
**Query Result**
RaghavGupta
.RaghavGupta
#Raghav.Gupta
"Raghav Gupta"
Raghav Gupta
Raghav#Gupta
Raghav$Gupta
Raghav%Gupta
Raghav*Gupta
Raghav.Gupta
"RaghavGupta "
RaghavGupta[]
You can use NOT with the matching regex:
select * from test_db.xtmp_testtable_invalidchars042321_rg where (sl_id NOT REGEXP '^[[:alnum:]]+([.-][[:alnum:]]+)*$')
The pattern matches
^ - start of string
[[:alnum:]]+ - one or more alphanumeric chars ([:alnum:] is a POSIX character class that matches letters and/or digits)
([.-][[:alnum:]]+)* - (a capturing group that matches) zero or more repetitions of
[.-] - a . or -
[[:alnum:]]+ - one or more alphanumeric chars
$ - end of string.

Regular expression to validate a specific pattern

I'm trying to create a regular expression with the below restrictions
Allow any character '\w\W\s' (e.g rand123#!##adfads)
Disallow only numbers (e.g 12312312)
Disallow only non alphanumeric characters (e.g !##$$#%#^%$^%)
Number of characters must be between 3 to 60
Following along the lines of this answer, but could not get it work.
^(?=.{3,60}$)(?![\W_]*$)(?![0-9]*$)[\w\W\s]+$
Note that \W matches \s, so '\w\W\s' can be reduced to [\w\W].
You may use 2 negative lookaheads anchored at the start to impose two "disallow-only" conditions like this:
^(?![\W_]+$)(?![0-9]+$)[\w\W]{3,60}$
See the regex demo
Pattern details:
^ - start of string
(?![\W_]+$) - the string cannot consist of non-alphanumeric chars
(?![0-9]+$) - the string cannot consist of digits only
[\w\W]{3,60} - 3 to 60 any characters
$ - end of string.

Regex - Match unlimited no. of words of fixed length

I'm fairly new to regex. I'm looking for an expression which will return strings in which the first character is of length 1, followed by an unlimited number of words of length 3 or more.
There should be a space between each word+.
So far I have:
([A-Za-z]{1,1} [A-Za-z+]{3,100})
As it stands this only returns phrases such as:
'I will' and 'A bird'
But I would like it to return phrases like:
'I will always try' and 'A bird flew into the cage'
Any help would be appreciated. I'm using an application called 'Oracle EDQ'.
You need to apply the limiting quantifier {3,} to the [A-Za-z] group and the * (zero or more repetitions) to the outer group matching a space + the 3+-letter words:
^[A-Za-z]( [A-Za-z]{3,})*$
See regex demo. Note the use of anchors ^ and $ that is very important when you need to match characters at a specific place (here, at the start and end of a word).
Regex matches:
^ - checks the regex engine is at the beginning of a string
[A-Za-z] - Exactly 1 letter a to z and A to Z
( [A-Za-z]{3,})* - zero or more sequences of...
- a space
[A-Za-z]{3,} - 3 or more ASCII letters
$ - end of string.
You can use this regex:
^[A-Za-z](?: [A-Za-z]{3,})+$
RegEx Demo

oracle regular expression restrict position of character from right

I am not able to proceed further and spent whole time to do google but didn't find a solution for my below query.
I want an Oracle regular expression that will meet the following requirements:
starts with ABCD followed by variable length alphanumeric with 'N'
present at 3nd or 4rd place from right.
E.g. ABCD201312102751N11 or ABCD201312102751N121.
The regex you can use is:
^ABCD[[:alnum:]]+N[[:digit:]]{2,3}$
EXPLANATION:
^ - Start of string
ABCD - Literal ABCD
[[:alnum:]]+ - Alphanumeric characters, 1 or more occurrences
N - a literal N
[[:digit:]]{2,3} - 2 or 3 digits (no more, no less)
$ - End of string.
See demo

What is the regex for "any number doesn't start with 0, but allows spaces in front and behind"

I have tried the following regex:
(([ ]+[1-9](\d+)?+[ ]+)|([ ]+[1-9](\d+)?)|[1-9](\d+)?[ ]+)|([1-9](\d+)?)
The strings I am testing against the regex are the following:
"Good" strings that should match:
" 3 "
"3"
"3 "
"Bad" strings that should fail:
" 03 "
"03"
"03 "
I have this so far, but the results I get are weird.
Simply you could try the below regex.
^\s*[1-9]\d*\s*$
You can use this regex:
^\s*[1-9][0-9]*\s*$
See demo
Explanation:
^ - Beginning of a line/string
\s* - Optional whitespace, any number of repetitions
[1-9] - A digit not equal to 0
[0-9]* - any number of digits
\s*$ - Trailing whitespace before string/line end
This regex will work almost with any regex engine, since not all of them know \d (although the majority recognizes this pattern.) Instead of \s, you can even use a literal space , but again, that all depends on the regex engine you will choose.
This will do
^\s*[1-9]\d*\s*$
But some have been faster than me to post.