Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions: Reducing duplication

Tags:

regex

Is there a way to simplify an expression such as this

([0-9]||[A-Z])([0-9]||[A-Z])([0-9]||[A-Z])([0-9]||[A-Z])([0-9]||[A-Z])([0-9]||[A-Z])

to something like {([0-9]||[A-Z]) = B } BBBBBB Where B represents a duplication of a sub expression.

I'm coding in labVIEW but I'm interested in a solution that is language independent.

like image 950
Usman Mutawakil Avatar asked May 19 '26 04:05

Usman Mutawakil


2 Answers

Sure, you can just:

([0-9]||[A-Z]){6}

The 6 refers to the number of items you'll accept. You can also:

([0-9]||[A-Z]){3,6}

Which would mean accept between 3 and 6 of these...

like image 122
Benj Avatar answered May 20 '26 17:05

Benj


Try this: ([0-9]||[A-Z]){6}

You can use {x} to designate the number of times you want it to show up. {6} means the pattern 6 times. You can use {1,6} to mean 1, 2, 3, 4, 5, 6 times.

One thing I like to have printed up next to my desk if I'm using Regular Expressions is this:

Regular Expression Cheat Sheet

It has saved me a number of times on syntax and such.

like image 42
Nick Avatar answered May 20 '26 16:05

Nick