Regex for text (string and numbers) between Pipes - regex

I have this scenario:
Ex1:
Valid:
12345678|abcdefghij|aaaaaaaa
Invalid:
12345678|abcdefghijk|aaaaaaaaa
Which means that between pipes the maximum length is 8. How can I make in the regex?
I put this
^(?:[^|]+{0,7}(?:\|[^|]+)?$ but it´s not working

Try the following pattern:
^.{1,8}(?:\|.{1,8})*$
The basic idea is to match between one and eight characters, followed by | and another 1 to 8 characters, that term repeated zero or more times. Explore the demo with any data you want to see how it works.
Sample data:
123
12345678
abcdefghi (no match)
12345678|abcdefgh|aaaaaaaa
12345678|abcdefghijk|aaaaaaaaa (no match)
Demo here:
Regex101

When you want to match delimited data, you should refrain from using plain unrestricted .. You need to match parts between |, so you should consider [^|] negated character class construct that matches any char but |.
Since you need to limit the number of the pattern occurrences of the negated character class, restrict it with a limiting quantifier {1,8} that matches 1 to 8 consecutive occurrences of the quantified subpattern.
Use
^[^|]{1,8}(?:\|[^|]{1,8})*$
See the regex demo.
Details
^ - start of a string
[^|]{1,8} - any 1 to 8 chars other than |
(?:\|[^|]{1,8})* - 0 or more consecutive sequences of:
\| - a literal pipe symbol
[^|]{1,8} - any 1 to 8 chars other than |
$ - end of string.
Then, the [^|] can be restricted further as per requirements. If you only need to validate a string that has ASCII letters, digits, (, ), +, ,, ., /, :, ?, whitespace and -, you need to use
^[A-Za-z0-9()+,.\/:?\s-]{1,8}(?:\|[A-Za-z0-9()+,.\/:?\s-]{1,8})*$
See another regex demo.

Related

Regex with wildcard search?

I created a Regex to check a string for the following situation:
first 4 chars are numbers
following by a point
following by 3 numbers
following by a point
following by 4 to 8 numbers or letters
ie: 1234.123.125B
My Regex: ^[0-9]{4}[.][0-9]{3}[.][0-9a-zA-Z]{4,8}$
But now I need a wildcard search: The Regex should also match if there is a '*' after the first 8 characters. For example:
1234.123.12* MATCH
1234.123* MATCH
1234.123.45B9* MATCH
1234.12* NO MATCH
1234.12345* NO MATCH
How can I add the wildcard search to my Regex?
Thank you
You may use this regex with alternation:
^\d{4}\.\d{3}(?:\*|\.[\da-zA-Z]{0,7}\*|\.[\da-zA-Z]{4,8})$
RegEx Demo
RegEx Details:
^: Start
\d{4}\.\d{3}: Match 4 digits + 1 dot + 3 digits
(?:\*|\.[\da-zA-Z]{0,7}\*|\.[\da-zA-Z]{4,8}): matches a single * OR a * after after a dot and 0 to 7 digits/letters OR match 4 to 8 digits/letters
$: End
My assumptions are that:
You don't allow wildcards to be mid-string
Nor do you want to allow wildcards after the full pattern (e.g.: 1234.123.12345678*).
So, alternatively you may possibily use something like:
^\d{4}\.\d{3}(?!.*\*.)(?![^*]{0,4}$)[.*][*\da-zA-Z]{0,8}$
See the online demo.
^ - Start string ancor.
\d{4}\.\d{3} - Four digits, a dot and another three digits.
(?!.*\*.) - Negative lookahead for zero or more characters followed by asterisk and another character other than newline.
(?![^*]{0,4}$) - Negative lookahead for zero to four characters other than asterisk before end string ancor.
[.*] - A literal dot or asterisk.
[*\da-zA-Z]{0,8} - Zero to eight characters from the character class.
$ - End string ancor.

Regular expression with alpha, hyphens, underscore and dots characters

I've wrote a regex:
/^[a-zA-Z\-\_\. ]{2,60}$/
It does work fine-ish however it allows --- or ___ or ... or even -_. to be entered as an input (without 2 alpha at least) and I don't want that. For instance I can have -aa, a-a, aa--- (similarly for the other characters).
The requirement is that there should be at least 2 alpha in the string, and the hyphens and the other 2 non-alpha symbols mentioned can be anywhere inside the string.
Use
/^(?=(?:[^a-zA-Z]*[a-zA-Z]){2})[-_. a-zA-Z]{2,60}$/
See the regex demo
Details:
^ - start of string
(?=(?:[^a-zA-Z]*[a-zA-Z]){2}) - at least 2 alpha chars in the string (that is, there must be exactly 2 consecutive occurrences of:
[^a-zA-Z]* - zero or more chars other than ASCII letters
[a-zA-Z] - an ASCII letter)
[-_. a-zA-Z]{2,60} - 2 to 60 occurrences of allowed chars
$ - end of string
Note you do not need to escape - if it is at the start/end of the character class. _ is a word char, no need escaping it anywhere. The . does not need escaping inside a character class.
To tell the regex engine to limit ., _ and - chars to max 10 in the string, add (?!(?:[^._-]*[._-]){11}) negative lookahead after ^ anchor:
/^(?!(?:[^._-]*[._-]){11})(?=(?:[^a-zA-Z]*[a-zA-Z]){2})[-_. a-zA-Z]{2,60}$/

Pipe separated values in groups of 3 regex

I have the following string
abc|ghy|33d
The regex below matches it fine
^([\d\w]{3}[|]{1})+[\d\w]{3}$
The string changes but the characters separated by the pipe are always in 3's ... so we can have
krr|455
we can also have
ddc
Here's where the problem happens: The regex explained above doesn't match the string if there is only one set of letters ... i.e. "dcc"
Let's do this step by step.
Your regex :
^([\d\w]{3}[|]{1})+[\d\w]{3}$
We can already see some changes. [|]{1} is equivalent to \|.
Then, we see that you match the first part (aaa|) at least once (the + operator matches once at least). Also, \w matches numbers.
The * operator matches 0 or more. So :
^(?:\w{3}\|)*\w{3}$
works.
See here.
Explanation
^ Matches beggining of string
(?:something)* matches something zero time or more. the group is non-capturing as you won't need to
\w{3} matches 3 alphanumeric characters
\| matches |
$ matches end of string.
^[\d\w]{3}(?:[|][\d\w]{3}){0,2}$
You simply quantify the variable part.See demo.
https://regex101.com/r/tS1hW2/18
You can modify your regex as below:
^([\d\w]{3})(\|[\d\w]{3})*$
here first match 3 alphaNumeric and then alphaNum with | as prefix.
Demo
Your description is a little awkward, but I'm guessing you want to be able to match
abc
abc|def
abc|def|ghi
You can do that with
/^\w{3}(?:\|\w{3}){0,2}$/
Visualization
Explanation
^ — match beginning of string
\w{3} — match any 3 of [A-Za-z0-9_]
(? ... )? — non-capturing group, 0 or 1 matches
\| — literal | character
$ — end of string
If the goal is to match any amount of 3-letter segments, you can use
/^(?:\w{3}(?:\||$))+$/

Regex to find 2 the same chars at the end of a string

I have to find a regex with following rules.
contains 8 to 20 chars (capital or normal).
contains no whitespace chars.
can't start with a number(0-9) or underscore (_).
at the end of the string it hase to be 2 of the same char.
must contain at least 1 number.
OK:
+234567899
a_1de*Gg
xy1Me*__
!41deF_hij2lMnopq3ss
C234567890123$^67800
*5555555
sDF564zer""
!!!!!!!!!4!!!!!!!!!!
abcdefghijklmnopq9ss
Not OK:
has more or less then 8-20 chars:
a_1+Eff
B41def_hIJ2lmnopq3stt
abCDefghijklmnopqrss5
has whitespace chars:
A_4 e*gg
starts with a number or underscore:
__1+Eff
841DEf_hij2lmnopq3stt
ends with two different chars:
a_1+eFg
b41DEf_hij2lmnopq3st
contains no numbers:
abCDefghijklmnopqrss
abcdef+++dF
!!!!!!!!!!!!!!!!!!!!
So far I have this
((?m:[^0-9_]^(?=.*[0-9])\S{8,20}$))
But I can't seem to figure out the 2 same chars at the end?
The following will work in most regex flavors (PCRE, Python, PHP, JavaScript):
/^(?=\S{8,20}$)(?=\D*\d)(?![0-9_]).{6,18}?(.)\1$/i
Demo with unit tests against your sample cases
Explanation:
/ delimiter
^ start of string
(?=\S{8,20}$) followed by 8-20 non-whitespace characters
(?=\D*\d) contains a digit
(?![0-9_]) can't start with a number or underscore
.{6,18}? non-greedy character match (moves us from the start of the string toward the end)
(.)\1 match any character, followed by the same character again
$ end of the string
/ delimiter
i flag: case-insensitive (required to see Gg, for example, as the same character twice)
The following one should suit your needs:
^(?=.*\d)[\D\S]\S{5,17}(\S)\1$
Visualization by Debuggex
Demo on regex101

Regular expression matching specific letter combos

I need to match the following example strings:
LA20517505
BN30116471
I tried this: [LA|BN].\d{8}
That does indeed match, but it also matches other letters as well. I specifically need to match "LA" or "BN" followed by 8 numbers.
Don't use brackets here but parenthesis : (LA|BN)\d{8}
Explanation:
(LA|BN) Match character sequences LA or BN
\d{8} followed by 8 digits
whereas the initial regex [LA|BN].\d{8} can be read as :
[LA|BN] Match either character L,A,|,B or N
. Match any character
\d{8} followed by 8 digits