0

Hi guys I am new with Regex I trying to define a pattern(python) in the following string:

\\xxxx\bbbbb\ccccc\mmmmmm\ooooooo\Snapshot\llllllllllllllllllll.png

S**p*ho* I need all words that have in position:

  • 0: have S
  • 1: any letter
  • 2: any letter
  • 3: have p or P
  • 4: any letter
  • 5: have h or H
  • 6: have o or O
  • 7: any letter

I am using:

res = re.findall(r'[S]\p{L}\p{L}\p{L}\p{L}\p{L}\p{L}\p{L}', str_3.upper())

But I also need specific characters in positions 0,3,5,6.

Thanks,

not_speshal
  • 22,093
  • 2
  • 15
  • 30
EdwinMald
  • 145
  • 1
  • 12
  • 2
    You're making it way harder than it is. To match the letter S, you just include the letter S, `r'S..[Pp].[Hh][Oo].'`. – Tim Roberts Nov 01 '21 at 18:58
  • 2
    `re.findall('(S[A-Za-z]{2}[Pp][A-Za-z][Hh][Oo][A-Za-z])', str_3)` . Don't use `upper` if you're checking both "P" and "p". – not_speshal Nov 01 '21 at 19:01
  • @TimRoberts I agree in general; yes, `S` is better than `[S]`. But `.` is not a valid translation for "any letter"; OP has it correct there (or at least they would if Python supported `\p`); and `0` is definitely not valid for `[oO]`. – Amadan Nov 01 '21 at 19:02
  • However if you `pip install regex` and `import regex as re`, you can use `\p{L}` just fine. – Amadan Nov 01 '21 at 19:08
  • No need for `regex`, use `[^\W\d_]` to match any Unicode letter. – Wiktor Stribiżew Nov 01 '21 at 19:20

1 Answers1

1

This is my first time answering a question, but I'll give it my best shot. Please try the following pattern and let me know if it helps.

S\p{L}\p{L}[pP]\p{L}[hH][oO]\p{L}

Here's an explanation for each part:

S matches exactly S.

\p{L} matches any kind of letter from any language.

[pP] matches p or P.

[hH] matches h or H.

[oO] matches o or O.

If \p{L} doesn't work, you can try to replace it with [a-zA-Z] which will match any lowercase or uppercase letter from a to z.

user17038038
  • 126
  • 7