Ivan Galin
Ivan Galin

Reputation: 35

The regular expression in python

Good day, just want to apologize for my English - the fact that I am using Google translator. Show you how in this regular expression in Python does not connect with the index named group 1 and group named 'ID111'?

This is the regular expression I am using:
r'(^RU)?(?P<ID111>(?(1).+|[\d]{5}))'

Here is the line where the search is performed:
RU Д-SK.ПК05.В.02695.jpeg (начинается с ТС)

Here is a link to convenient service to check with the already exposed data a regular expression:
https://regex101.com/r/mDh0kZ/1

The fact is that if a line where we searched if returns true, then the group 'ID111' misses the entire line as intended and only what comes after a group of index 1. I want to solve it with a regular expression only.

I would be very grateful for any help.

Upvotes: 1

Views: 119

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

As far as I can see, the conditional is not even necessary, all you need is use 2 alternatives: 1) match a string from the start if it starts with RU up to the last ., or 2) match a 5-digit chunk.

Thus, use

r'(?P<ID111>^RU.*(?=\.)|\d{5})'

See the Python regex demo

Details:

  • ^RU - a RU at the start of the string
  • .*(?=\.) - any 0+ chars other than line break chars up to the last . (excluding it)
  • | - or
  • \d{5} - 5 digits (note that to match a 5-digit chunk not enclosed with other digits, use (?<!\d)\d{5}(?!\d)).

Upvotes: 1

Related Questions