0

Hi I have a requirement where bunch of expressions should not be accepted which includes white spaces as well.

Below are the expressions.

  1. PA#N<number>.<number>#ADP<number> e.g. PA#N1.1#ADP1
  2. PA#N<number>.<number>,<number>.<number>#ADP<number> e.g. PA#N1.1,1.2#ADP1,2
  3. PA#N<number>#ADP<number> e.g. PA#N1#ADP4
  4. PA#N<number>,<number>#ADP<number> e.g. PA#N1,2#ADP4
  5. PA#N<number>,<number>.<number>,<number>#ADP<number> e.g PA#N1,2.1,3#ADP1
  6. PA#N<number>,<number>.<number>-<number>#ADP<number> e.g. PA#N1.1-3#ADP1
  7. PA#N<number>,<number>.<number>-<number>.<number>#ADP<number> e.g. PA#N1.1- 3.2#ADP1,2
  8. PA#NUAL,AZ,AN#FOP<number>
  9. PA#N1.1-2.1,3.1#ADP#PUSH
  10. PA*N1.1*ADP

and if it is having any spaces at PA#<space>N<space>1<space>1#ADP also not acceptable, but PA#N1.1...#5#SOME TEXT (the SOME TEXT part after last '#' with free text with free spaces are acceptable).

PA* also not acceptable.

For all these the above conditions to be satisfied , I created a regex

PA(?!\*$)(?!([#*].*\s+).*)(?!(\*N(\d+(\.\d+)?,?)+)$)(?!([#*](U.*|T.*|(N\d+\.?\d*)-(\d+\.?\d*)|(N(\d+(\.\d+)?,?)+))){1,3}[#*]ADP).*

All the above conditions are matched with the above regex, only thing missing is PA#N1.1#5#FREE TEXT (if we give any space between FREE<SPACE>TEXT then it is not accepting.

If we don't give any space between FREE TEXT like FREETEXT then that matches with above regex.

Could anybody help to find my mistake and what would be the correct expression? I have been struggling with this 2 days.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
vissi
  • 43
  • 1
  • 1
  • 4

1 Answers1

0

I would suggest two things to simplify the solution:

  1. Assume that all the combinations between PA#N and #ADP can be described as a series of 1 to 6 numbers delimited by ether ., or ,, or - and optionally surrounded by spaces. This can be expressed by the \s*\d+\s*(?:[\.,-]\s*\d+\s*){0,5}. I'm not sure if this assumption is acceptable for you, but if it is, this would simplify the the regex a lot. If the assumption is not acceptable, I provided a link to the extended regex in the end of the answer.

  2. Write a pattern to match all the strings you have to exclude and then invert it using the approach described here. In brief it looks like this: /^((?!INVERTED_PATTERN).)*$/

The expression in this case would be:

^((?!      # This and the final line allow invert the regex condition

PA(?:
    \*(?:N\s*\d+\s*\.\s*\d+\s*\*ADP)?    # match the asterisk cases
  | \#\s*N\s*\d+\s*                      # or the hash cases
        (?:[\.,-]\s*\d+\s*){0,5}
    \#ADP\s*(?:\d|\#PUSH)
  | \#NUAL,AZ,AN\#FOP\d                  # or the 'UAL,AZ,AN' literal
)

).)*$

Note: this regex requires the x modifier to be used.

Demo: https://regex101.com/r/bY4xU6/3

If the first assumption is not acceptable, you may use more elaborated regex from here: https://regex101.com/r/oD8uG7/1

Community
  • 1
  • 1
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40