-1

I need to find string with length of 12 and contains in the 7 position the char "U".

I have the following strings:

SUPERHUMANLY
DFGDFGDFGGFG
SUPSRHTMANLY
DFGDFGUFGGFG

Only the following should matched:

SUPERHUMANLY
DFGDFGUFGGFG

Following this link I know how to get match on the 7 position:

^.{6}[U]

But I want to get match on only letters with specific length, so I tried ^.{6}[U]{12}, ^(.{6}[U]){12} and ^.{6}[U].{12} without success.

How can I combine the length of the string with the position ?

E235
  • 6,640
  • 11
  • 56
  • 99
  • 1
    If you are not limited to regex, you may use `[s for s in my_strings if len(s) == 12 and s[6] == "U"]` ([demo](https://ideone.com/gaVc3j)) – Wiktor Stribiżew Sep 26 '18 at 10:45

1 Answers1

3

How about simple :

^.{6}U.{5}$
Toto
  • 83,193
  • 59
  • 77
  • 109
  • 2
    Some explanation to answer would be great. :) – CappY Jul 16 '20 at 13:01
  • 1
    @CappY: Such basic regex doesn't really need explanation. `.` stands for any character but newline and `{5}` stands for 5 occurrences of previous character. – Toto Jul 16 '20 at 14:16