0

How can i name a group of pattern and use it many times in the whole expression ? I have for example some regular expression like :

([a][b|c|d|e])[x|y|z][p|q|r]([a][b|c|d|e]) ......([a][b|c|d|e]).... ([a][b|c|d|e])

In the above, if you notice [a][b|c|d|e] has been used many a times. I want to name this group to something, so that expression can become more readable. Is that possible ?

I am looking for something so that once the group ([a][b|c|d|e]) is named say "abcdgroup" then i can use "abcdgroup" everywhere in the whole expression. Also, It should not save the result of 1st group, but should solve every time it's used.

HamZa
  • 13,530
  • 11
  • 51
  • 70
Vishwas
  • 1,472
  • 2
  • 19
  • 40
  • Not aware of any way to do this as a straight regex, but you might be able to utilize the string concatenation features of your language of choice to get the same effect. – esqew Nov 16 '14 at 21:31
  • actually, my aim is to somehow reduce the length of the whole expression. – Vishwas Nov 16 '14 at 21:40
  • @VishwasG What tool/language/engine are you using? It's possible in PCRE. – HamZa Nov 16 '14 at 21:41
  • as3.0 ( actionscript ) – Vishwas Nov 16 '14 at 21:42
  • @VishwasG Then the answer is: no, you can't do it directly with regex. A workaround is to use string concatenation like [@esqew](http://stackoverflow.com/users/269970/esqew) said. Here's a PCRE/perl regex for the ideal situation: `(?P\d+)(?Pfoo)(?&digits)(?&foo)(?&digits)`. [Demo](http://regex101.com/r/mH1uJ4/1). – HamZa Nov 16 '14 at 21:46
  • Basically it's called recursion. A nice explanation can be found [here](http://stackoverflow.com/questions/18149814/is-there-a-way-to-define-custom-shorthands-in-regular-expressions/18151617#18151617) – HamZa Nov 16 '14 at 21:47
  • How string concatenation is related to this ? – Vishwas Nov 16 '14 at 21:49
  • @VishwasG I'm not an AS developer so let's talk in pseudo code. You might use `String digitRegex = "\d+"; String fooRegex = "foo"; String finalRegex = digitRegex + fooRegex + digitRegex";` instead of `String finalRegex = "\d+foo\d+";` – HamZa Nov 16 '14 at 21:58

0 Answers0