0

I am busy with the following code:

^( ?(?!sa|sd|ss|SA|SD|SS)[A-Za-z]{2})[1][7][9][0]\1|[8][8][8][8]\1 $

So what i'm trying to create it that the first capturing group, it creates a-z 2 letters but then without sa/sd/ss/SA/SD/SS, that the group joins the [1][7][9][0] or the [8][8][8][8]

So that for example 1790AA is a match and 8888AA is a match. I could just do :

^[1][7][9][0-7] ?(?!sa|sd|ss|SA|SD|SS)[A-Za-z]{2}$|[8][8][8][8] ?(?!sa|sd|ss|SA|SD|SS)[A-Za-z]{2}$

but as you can see it will be a long one. Is it possible to use something like: ^( ?(?!sa|sd|ss|SA|SD|SS)[A-Za-z]{2}) (creating the group) then [1][7][9][0] + \1 $

  • Yes, it is a very simple thing in PCRE. Create a capturing group and use a subroutine in the form of `(?n)` or `\g` where `n` is the Group ID. – Wiktor Stribiżew Jun 15 '18 at 12:27
  • Thanks for your answer, sadly it isn't that simple, because it doesn't work. – Willem Munts Jun 15 '18 at 12:32
  • What you ask is that simple. If your pattern does not work, it *didn't* work in the first place. Check your question and update with the real issue description. – Wiktor Stribiżew Jun 15 '18 at 12:33
  • My code works, allready tested it. Did you test it? – Willem Munts Jun 15 '18 at 12:37
  • Well, as I said, https://regex101.com/r/ShvlyL/2 or even a more "obfuscated" https://regex101.com/r/ShvlyL/1 works. Not sure what you are using. I would recommend shortening to https://regex101.com/r/ShvlyL/3 though. Or even to https://regex101.com/r/ShvlyL/4 – Wiktor Stribiżew Jun 15 '18 at 12:39
  • Well, that's exactly the one that i needed, i did found the other question, but i still couldn't combine it with mine, so i thought i might ask it again in my way, thanks! – Willem Munts Jun 15 '18 at 12:42
  • The only thing you need to know is 1) create a capturing group wrapping the part you need to repeat, 2) use the subroutine call. It has already been explained. – Wiktor Stribiżew Jun 15 '18 at 12:43
  • Yes, i did read that part, just didn't know how to combine it with my piece of code, thank you very much! – Willem Munts Jun 15 '18 at 12:45
  • Only thing i am wondering, why did you put the {0} there? – Willem Munts Jun 15 '18 at 12:50
  • You do not need to use that example. It is not a good idea. Just use the pattern where necessary, wrap with parentheses, refer to it in the other part. I shared 4 links, use [the last one](https://regex101.com/r/ShvlyL/4). – Wiktor Stribiżew Jun 15 '18 at 12:51
  • oh i see yea, only thing is, that i have it with more, 1790 - 1797 and 8899 and 8881 - 8897 and 9161 - 9164 and 9166 need to be a match. So what i used now is: `[1][7][9][0-7]\g<1>|[8][8][8-9][^08]\g<1>|[8][8][8][8]\g<1>|[9][1][6][1-46]\g<1>` that works OK – Willem Munts Jun 15 '18 at 12:55
  • Ok, I see you have a problem other than just shortening: please update the question if you need to get an answer: 1) provide the pattern, 2) strings and their expected outputs, 3) what pattern you are using and what is wrong with the current approach. Do not wrap single chars with `[...]`, it is pointless and harmful sometimes. – Wiktor Stribiżew Jun 15 '18 at 12:57

0 Answers0