How to write repeating Regex pattern in JavaScript - javascript

How to write a regex for the following pattern in JavaScript:
1|dc35_custom|3;od;CZY;GL|2;ob;BNP;MT|4;sd;ABC;MT|5;ih;DFT;FR|6;oh;AQW;MT|7;ip;CAN;MT|8;op;CAR;MT|9;ec;SMO;GL|10;do;CZT;KU|
where
the first part 1|dc35_custom| is fixed.
the second part onwards, the pattern repeats 9 times(i.e. 3;od;CZY;GL| 2;ob;BNP;MT| and so on.
The 1st character in it ranges from 2-11 and should not repeat. For example 3 appears in the first pattern, so should not appear again.

I'm making a lot of assumptions with this, but here's a crack at it:
1\|dc35_custom\|(([2-9]|10|11);[a-z]{2};[A-Z]{3};[A-Z]{2}\|){9}
How it works
1\|dc35_custom\| is just literal text, escaping the vertical bar operators
([2-9]|10|11) will match any number from 2 to 11.
[a-z]{2} will match two lowercase letters
[A-Z]{3} will match three uppercase letters
[A-Z]{2} will match two uppercase letters
{9} looks for nine consecutive matches of the entire sequence enclosed in parentheses
It will not, as Amadan points out, check for uniqueness, because that's a bit beyond what regex is for.

A bit tricky, but here you go
The regex: /^1\|dc35_custom(?:\|([2-9]|1[01]);[a-z]{2};[A-Z]{1,3};[A-Z]{1,2}){9}\|$/
and the Unit tests: https://regex101.com/r/lU6sJ6/2 (hit 'Unit Tests' on the left)
I assume the following:
the first group WILL ALWAYS BE THE SAME
The first part of the pattern 3;od;CZY;GL is a number between 2-11 and NO NUMBER CAN REPEAT
The second part is lowercase letters a-z, exactly two of them
The third part is uppercase letters A-Z, between 1 and 3 ( the {1,3} thing, you can change it to {3} if it's exact)
The fourth and last part is between 1 and 2 uppercase letters A-Z

Related

Regex pattern to find all the digits which don't have the immediate dot character

Can any of you please help me to write a regex pattern for the below requirement?
Section tags that don't have numbers
All section tag numbers that don't have a dot character followed by.
Numbers that are closer to the section tag only that to be considered.
Test String:
<sectionb>2.3. Optimized test sentence<op>(</op>1,1<cp>)</cp></sectionb>
*<sectiona>2 Surface Model: ONGV<op>(</op>1,1<cp>)</cp></sectiona>*
<sectiona>3. Verification of MKJU<op>(</op>1,1<cp>)</cp> Entity</sectiona>
*<sectionc>3. 2. 1 <txt>Case 1</txt> Annual charges to SGX</sectionc>*
*<sectiona>Compound Interest<role>back</role></sectiona>*
Pattern:
<section[a-z]>[\d]*[^\.]*<\/section[a-z]
Regex Pattern Should Match the below string:
<sectiona>2 Surface Model: ONGV<op>(</op>1,1<cp>)</cp></sectiona>
<sectionc>3. 2 1 <txt>Case 1</txt> Annual charges to SGX</sectionc>
<sectiona>Compound Interest<role>back</role></sectiona>
This matches the updated requirements:
<section\w+>(((\d+\.\s*)*(\d+[^\.]))|[^\d]).*?<\/section\w>
<section\w+> \w is mostly the same as [a-z] with + to allow for 0 or more (<section> <sectionabc>), remove + for exactly one letter
(\d+\.\s*)* 0 or more digit/dot/any number of spaces - match updated row 3 where it's now 3. 2. 1 with spaces after dots
(\d+[^\.]) must match digit without a dot, one or more digits
((...)|[^\d]) or section does not start with a digit (match row 5)
.*? followed by any character, as few as times as possible upto the following </section - could likely do this with a look ahead to simplify the regex, but, for me, this keeps the separate "no digits" clause separate.
regex101

Regex for match beginning with 2 letters and ending with 3 letters

Example input:
'Please find the ref AB45676785567XYZ. which is used to identify reference number'
Example output:
'AB45676785567XYZ'
I need a RegExp to return the match exactly matching my requirements; i.e. the substring where the first 2 and last 3 characters are letters.
The first 2 and last 3 letters are unknown.
I've tried this RegExp:
[a-zA-Z]{2}[^\s]*?[a-zA-Z]{3}
But it is not matching as intended.
Your current RegExp matches the following words marked with code blocks:
Please find the ref AB45676785567XYZ. which is used to identify reference number
This is because your RegExp, [a-zA-Z]{2}[^\s]*?[a-zA-Z]{3}, is asking for:
[a-zA-Z]{2} Begins with 2 letters (either case)
[^\s]*? Contains anything that isn't a whitespace
[a-zA-Z]{3} Ends with 3 letters (either case)
In your current example, restricting the letters to uppercase only would match only the match you seek:
[A-Z]{2}[^\s]+[A-Z]{3}
Alternatively, requiring numbers between the 2 beginning and 3 ending letters would also produce the match you want:
[a-zA-Z]{2}\d+[a-zA-Z]{3}
What is really important here, is word boundaries \b, try: \b[a-zA-Z]{2}\w+[a-zA-Z]{3}\b
Explanation:
\b - word boundary
[a-zA-Z]{2} - match any letter, 2 times
\w+ - match one or more word characters
[a-zA-Z]{3} - match any letter, 3 times
\b - word boundary
Demo
CAUTION your requirements are amibgious, as any word consisting of 5 or more letters would match the pattern
Start with 2 letters :
[a-zA-Z]{2}
Digits in the middle :
\d+
Finish with 3 letters :
[a-zA-Z]{3}
Full Regex :
[a-zA-Z]{2}\d+[a-zA-Z]{3}
If the middle text is Alpha-Numeric, you can use this :
[A-Z]{2}[^\s]+[A-Z]{3}

Testing string for words with 3 repeated consecutive characters

I'm trying to get a Regex expression that will be satisfied when given a string that has at least 1 word composed of 3 or more repeating consecutive characters, and no other characters:
Testing AAAAAA Test - Valid
Testing AAAAAAB Test - Invalid
The previous solution I had reached was not enough to recognize if there were different characters in the word:
/^(?:(.)(?!\1{2}))+$/gi
This was essentially just testing if the 2 characters after each character are equal to it.
Any ideas?
How about
\b(.)\1{2,}\b
which is
\b word boundary
(.) something
\1 previous thing...
{2,} ...twice or more
\b word boundary
https://regex101.com/r/BKHkOc/3
Add word boundaries. And use {2,} to match at least 2 repetitions.
/\b(.)\1{2,}\b/
There's no need for i, since you're not matching letters, so case is irrelevant. And g is not needed when just testing; it's only useful if you're returning all the matches or doing a replacement.

Regex to match a rather complex string

Any experts on Regex, that could potentially find a pattern on this data, im looking for one that will match exactly, down to spaces and commas and dashes. Here is the sample data of what i need to match:
word word, alphanumeric-PRT-word-number
word word, alphanumeric-PRT-number
-word: any size word
-alphanumeric: 3 letters and up to 2 numbers, so XXX# or XXX##
-number: up to 3 digits, so # or ## or ###
-PRT: is the only static value here
NOTE: no other punctuation other than the spaces, comma and dashes where they are.
So far have something close to it but rather clunky and it doesnt cover all bases, i built it here: http://buildregex.com/ using their logic and it kinda works:
/(?:[^_\ ]+)(?:\ )(?:[^_\ ]+), (?:[^_\ ]+)-PRT-(?:[^_\ ]*)/gi
If any can assist in refining this that will be welcome
https://regex101.com/r/8cc52u/2
Thanks a lot
Here's one way to do it:
/^[a-z]+\s[a-z]+,\s[a-z]{3}\d{1,2}-prt-([a-z]+-){0,1}\d{1,3}$/gi
^: start of line
[a-z]+: one or more letters
\s: any space character
[a-z]+: one or more letters
,: ,
\s: any space character
[a-z]{3}: three letters
\d{1,2}: one or two digits
-prt-: -prt-
([a-z]+-){0,1}: one or more letters followed by -, zero or one time
\d{1,3}: one, two or three digits
$: end of line
Example: https://regex101.com/r/BhS8kM/5
Or, as suggested by revo:
/^[a-z]+ [a-z]+, [a-z]{3}\d{1,2}-prt-([a-z]+-)?\d{1,3}$/gi
Example: https://regex101.com/r/BhS8kM/7

Regex - At least 1 number, 1 letter, 1 special character and at least 3 characters

I wanna test my own regex in order to understand how can i use it and create my custom regex.
I tried this one :
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]){3}$
(?=.*\d) => at least 1 number.
(?=.*[a-zA-Z]) => at least 1 letter.
(?=.*[\W_])=> at least 1 special character.
{3} => at least 3 characters.
Unfortunately, it doesn't work, but i want at least 1 number, 1 letter and 1 special character, and at least 3 characters in my input. When the user types those 3 types of characters, my message disappears because the regex is correct ^^
Sorry for my bad english, I can give you more details if you want and thank you for the help :)
Have a nice day :)
The problem is that your {3} quantifier applies to your last look-ahead, which is nonsensical : it means that at the start of the text, you must match 3 times the look-ahead, which is a given if it matches once since lookaround are 0-width matches.
You could use the following :
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3,}$
which specifies, aside from your existing look-aheads, that at least 3 characters must be matched.
If you just test the string, it would also be enough to match
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3}
without an end anchor : you match 3 characters, and stop matching what follows since your requisites are met.
If you want 1 digit, 1 alpha, 1 special character, those are already at least 3 characters.
^(?=\D*\d)(?=.*?[a-zA-Z]).*[\W_].*$
Here's a demo at regex101. Or for only matching:
^(?=\D*\d)(?=.*?[a-zA-Z]).*[\W_]
(?=\D*\d) The first lookahead requires one digit (preceded by any \D non-digits).
(?=.*?[a-zA-Z]) second lookahead requires one alpha (preceded by any characters).
.*[\W_] matching until one special character.
All together requires at least 3 different characters: 1 digit, 1 alpha, 1 special.

Categories

Resources