How to recreate this reg expression to fit more conditions? - regex

Regular expression:
/(\b|[x])\d{7}\b/
Essentially what I'm trying to do is only highlight the digits and not the letter "x". I also would like to highlight the 7 digits even if it has multiple spacse between the main number and the 7 digits. I seem to be only be able to highlight expressions when its followed by exactly 1 space.
Thanks!

So you want to highlight exactly 7 digits that are followed by space or x?
Use this pattern.
(?<=[x ])\d{7}(?!\d)
(?<=[x ]) is lookbehind. means only match 7 digits when the preceding character is x or whitespace.
(?!\d). is negated lookahead. should be exact 7 digits. so the next character after 7 digits should not be digit.

Related

Regular Expression that needs to match three exact digits in a four digit number

The regular expression that I am trying to create should match all numbers that contain three '8's in any 4 digit number. The regular expression that I have only matches the first 10 numbers out of the list of 15 numbers. Any suggestions will be greatly appreciated.
\b[0-9]*(?:8[0-9]*[0-9]?8|8[0-9]*[0-9]?8|8[0-9]*[0-9]?8)\b
Test data:
8088 8188 8288 8388 8488 8808 8818 8828 8838 8848 8880 8881 8882 8883 8884
The last five numbers should also match, but don't.
You can use
\b(?=\d{4}\b)(?:[0-79]*8){3}[0-79]*\b
See the regex demo.
Details:
\b - a word boundary
(?=\d{4}\b) - there must be 4 digits immediately on the right and they should be followed with a word boundary
(?:[0-79]*8){3} - three occurrences of any 0 or more digits but 8 and then 8
[0-79]* - any 0 or more digits but 8
\b - word boundary.
If it's guaranteed that the number is a four-digit number, then you can try the following:
\b8*[0-79]8*\b
To analyze what each part matches, you can check using,
\b(8*)[0-79](8*)\b
This should do it. This will match any of the 4 patterns.
([\d888]|[8\d88]|[88\d8]|[888\d])
You may want to add a check for the delimiter (in your example the space) as this pattern will match across the spaces giving you many more results
\b(\d?8{3}\d?)\b
this makes the first and last digit in the word bound optional, use
either ? or {0,1}
add quantifier to your eight to have exactly
number of eights you need {3}
replace [0-9] with \d as
Digit for brewity
supposed you have only numbers of length 4. Otherwise use an alternative without optional digits: \b(\d8{3}|8{3}\d)\b

regex few blocks exact length

I want to match 5 to 20 character with regex.
I try to use below regular expression for my checking.
/^[a-zA-Z][\w]{5,20}$/
It's work, but the problem of length it match 6 to 21 character.
(^[a-zA-Z][\w]){4,20}$
I also try this but it don't work.
Please anyone help me to match exact length of regex.
It's because your capturing group is expecting TWO characters:
[a-zA-Z] and [\w], that's two letters.
So your first attempt actually did this:
match [a-zA-Z] once
match [\w] once
match the previous matches 5 - 20 times
Inevitably, you always had 1 more match than expected
Capture only one character, and iterate it 5-20 times.
Have you tried:
^([a-zA-Z]{5,20})$ ?
OR
^(\w{5,20})$ ?
You're almost there, you just need to make a single range of characters (in square brackets) not two.
/^[a-zA-Z][\w]{5,20}$/ means:
a character from a to z in lower or upper case
5 to 20 word characters
That sums up to 6 to 21 characters in total.
I suppose you want /^[a-zA-Z][\w]{4,19}$/:
a character from a to z in lower or upper case
4 to 19 word characters
That sums up to 5 to 20 characters in total.
The Quantifier is only applied to the [\w]. So this expects exactly one letter character and then 5-20 whitespace characters.
I assume you want 5-20 characters that can be either a letter a-z or a whitespace. You need to group these together in square brackets and then apply the quantifier:
^[a-zA-Z\W]{5,20}$
So, I understand, you want a string that has 5-20 characters, starts with a letter and then only has letters and digits. You would write it like that:
^[a-zA-Z][a-zA-Z0-9]{4,19}$
This expects first a letter and then 4-19 letters or digits.
BTW: https://regex101.com/ is a great site to test regular expressions and get an explanation what they are doing.

How can I recognize a valid barcode using regex?

I have a barcode of the format 123456########. That is, the first 6 digits are always the same followed by 8 digits.
How would I check that a variable matches that format?
You haven't specified a language, but regexp. syntax is relatively uniform across implementations, so something like the following should work: 123456\d{8}
\d Indicates numeric characters and is typically equivalent to the set [0-9].
{8} indicates repetition of the preceding character set precisely eight times.
Depending on how the input is coming in, you may want to anchor the regexp. thusly:
^123456\d{8}$
Where ^ matches the beginning of the line or string and $ matches the end. Alternatively, you may wish to use word boundaries, to ensure that your bar-code strings are properly separated:
\b123456\d{8}\b
Where \b matches the empty string but only at the edges of a word (normally defined as a sequence consisting exclusively of alphanumeric characters plus the underscore, but this can be locale-dependent).
123456\d{8}
123456 # Literals
\d # Match a digit
{8} # 8 times
You can change the {8} to any number of digits depending on how many are after your static ones.
Regexr will let you try out the regex.
123456\d{8}
should do it. This breaks down to:
123456 - the fixed bit, obviously substitute this for what you're fixed bit is, remember to escape and regex special characters in here, although with just numbers you should be fine
\d - a digit
{8} - the number of times the previous element must be repeated, 8 in this case.
the {8} can take 2 digits if you have a minimum or maximum number in the range so you could do {6,8} if the previous element had to be repeated between 6 and 8 times.
The way you describe it, it's just
^123456[0-9]{8}$
...where you'd replace 123456 with your 6 known digits. I'm using [0-9] instead of \d because I don't know what flavor of regex you're using, and \d allows non-Arabic numerals in some flavors (if that concerns you).

What does this regular expression mean?

could somebody please tell me what the following expressions means:
\d{6,8}
As far as I know it's a regular exp
Between 6 and 8 numeric digits.
(As it's not anchored to boundaries or start & end of string, it would also match between 6 and 8 digits within a longer series of digits - for instance, it will match 123456, 1234567, 1234678, but also the first 8 digits of 123456789.)
\d is a character class - it could also have been written as [0-9]. The {} part is a repetition count; it could be a single number, e.g. {6}, or, as in this case, a range - so the {6,8} means "the previous thing, repeated between 6 and 8 times".
it matches between 6 and 8 sequential numeric digits.
\d is equivalent to the character class [0-9], and the {,} notation specifies an exact number of times that a pattern has to match.
matches a digit that is of length between 6 and 8
it means, at least 6 digits and no more than 8 digits
It means between 6 to 8 numbers in a row.
\d means a number [0-9]
{6, 8} means min. of 6, max. of 8
You use curly braces to describe how many of the previous character you want to look for. Entering a single number, like {3} means 3 in a row. Adding a second number changes this into min/max.
http://www.regular-expressions.info
is the best site on the web for learning about regular expressions.

6 digits regular expression

I need a regular expression that requires at least ONE digits and SIX maximum.
I've worked out this, but neither of them seems to work.
^[0-9][0-9]\?[0-9]\?[0-9]\?[0-9]\?[0-9]\?$
^[0-999999]$
Any other suggestion?
You can use range quantifier {min,max} to specify minimum of 1 digit and maximum of 6 digits as:
^[0-9]{1,6}$
Explanation:
^ : Start anchor
[0-9] : Character class to match one of the 10 digits
{1,6} : Range quantifier. Minimum 1 repetition and maximum 6.
$ : End anchor
Why did your regex not work ?
You were almost close on the regex:
^[0-9][0-9]\?[0-9]\?[0-9]\?[0-9]\?[0-9]\?$
Since you had escaped the ? by preceding it with the \, the ? was no more acting as a regex meta-character ( for 0 or 1 repetitions) but was being treated literally.
To fix it just remove the \ and you are there.
See it on rubular.
The quantifier based regex is shorter, more readable and can easily be extended to any number of digits.
Your second regex:
^[0-999999]$
is equivalent to:
^[0-9]$
which matches strings with exactly one digit. They are equivalent because a character class [aaaab] is same as [ab].
^\d{1,6}$
....................
You could try
^[0-9]{1,6}$
it should work.
^[0-9]{1,6}$ should do it. I don't know VB.NET good enough to know if it's the same there.
For examples, have a look at the Wikipedia.
\b\d{1,6}\b
Explanation
\b # word boundary - start
\d # any digits between 0 to 9 (inclusive)
{1,6} # length - min 1 digit or max 6 digits
\b # word boundary - end
^[a-zA-Z0-9]{1,6}$
regex 6 digit number and alphabet in angular
/^[0-9][0-9][0-9][0-9]$/
Enter 4 digit number only