Regex to check for repetition

greywolf82

I need to check a string that must match:

1,2,3,4,5,6,7

So the rules are: only one number between 1 and 7, a comma separator, but I don't want repetition, so it's not legal to write:

1,2,3,1,5

Currently I'm using:

([1-7]{1},){1,6}([1-7]{1})

How can I modify it in order to check for repetition?

gfullam

I think @tobias_k is on the right track by suggesting that it's a good strategy to check for strings that have repetition, but I think you can have a more comprehensive regular expression that limits the character class and delimiter as you require.

Update: This version checks for occurrence of multiple digits.

([1-7]{1})(?:[1-7,])*\1|[^1-7,]+
([1-7]{1}),(?:[1-7]{1},)*(?:\1|[^1-7,]|(?:[1-7]{2,}))

1,2,3,4,5,6,7    // PASS: no match
1,2,3,4,5,6,8    // FAIL: matches out-of-bounds digit '8'
1,2,3,four,5,6,7 // FAIL: matches non-integer 'four'
1,2,3,1,5        // FAIL: matches repeating digit '1'
1,2,3,45         // FAIL: matches multiple digits

Syntax diagram via regexper.com

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex repetition

From Dev

Check for repetition in input (Python)

From Dev

Nongreedy regex with alternation and repetition

From Dev

how to replace repetition in regex?

From Dev

PHP Regex, capture repetition matches

From Dev

capture repetition of letters in a word with regex

From Dev

Best practice for regex to match repetition

From Dev

PHP Regex, capture repetition matches

From Dev

capture repetition of letters in a word with regex

From Dev

Regex - string repetition of min length

From Dev

Regex capture group reference > repetition?

From Dev

nginx rewrite regex min,max repetition

From Dev

Regex to SQL: repetition-operator operand invalid

From Dev

regex pattern repetition and ng-invalid

From Dev

A Regex to match any sentence but avoiding character repetition

From Dev

C# Regex And Curly Braces For Repetition

From Dev

Regex match repetition of a character OR single character

From Dev

How do I signify repetition in a ZMV regex?

From Dev

How to prevent char repetition with Java regex

From Dev

Regex to identify HTML tags (as a regex repetition learning exercise ONLY!!)

From Dev

Regex to identify HTML tags (as a regex repetition learning exercise ONLY!!)

From Dev

Regex: Repeat pattern n times, with variation on final repetition

From Dev

Caused by: java.util.regex.PatternSyntaxException: Illegal repetition

From Dev

Is it possible to not limit the upper value of a regex range for a repetition of a character (Infinite)?

From Dev

Regex check sentence for a word

From Dev

Regex for Url Check

From Dev

Regex to check if username is valid or not

From Dev

Regex to check with starts with [ and end with ]

From Dev

Check if expression matches a regex

Related Related

HotTag

Archive