I have to check many strings which are 14 characters long. There should be at least one occurrence of 'F' in a 14 character string but not at 7th position.
Currently, I'm doing this in 2 steps.
First, check if 'F' is not present at position 7 and save them.
^.{6}[^F].{7}$
Then check if there is at least one occurrence of 'F' in the saved strings.
^[F] $
Is there a better way to do this?
CodePudding user response:
If supported, you can make use of lookarounds asserting an F
and not match it on the 7th position:
^(?=[^F\n]*F)(?:.{6}[^F].{7})$
CodePudding user response:
Just as an alternative:
^(?!.{6}F|[^F] $).{14}$
See an online demo
^
- Start-line anchor;(?!.{6}F|[^F] $)
- Negative lookahead with alternation to avoid a line with an 'F' at index 7 or a line with no 'F' at all;.{14}$
- 14 Characters other than newline upto end-line anchor.