dmorgan
dmorgan

Reputation: 73

error: multiple repeat at position while using a regex pattern

I'm using a regex to replace any string that appears after the word username followed by a (: or ;) with optional spaces with one optional space in between.

I use the below regex in PHP:

(?i)\bUsername\s?+(:|;)\s?+\K\S+

However I wanted to know how to use the same expression in python as I get the error: error: multiple repeat at position 17".

Below is my testcase and I wanted to extract only dasdsad from all the examples.

Any ways to achieve this in Python?

Username:dasdsad
username ;dasdsad
username : dasdsad
username; dasdsad

Upvotes: 1

Views: 3644

Answers (2)

Ryszard Czech
Ryszard Czech

Reputation: 18641

Use

(?i)\bUsername\s*[:;]\s*(\S+)

See proof. Instead of \K, use capturing group, (\S+) will capture one or more non-whitespace characters into a group.

Python demo code:

import re

regex = r"(?i)\bUsername\s*[:;]\s*(\S+)"

test_str = ("Username:dasdsad\n"
    "username ;dasdsad\n"
    "username : dasdsad\n"
    "username; dasdsad")

print( re.findall(regex, test_str) )

UPDATE:

To replace and keep part of match use

test_str = re.sub(r'(?i)(\bUsername\s*[:;]\s*)\S+', r'\1username', test_str)

If you need to replace with digit:

test_str = re.sub(r'(?i)(\bUsername\s*[:;]\s*)\S+', r'\g<1>1234', test_str)

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163577

Python does not support possessive quantifiers or \K.

If you want to replace the matches, you don't need \s?+.

You could use \s* and use \K, and make use of the regex pypi module and use regex.sub

\bUsername\s*[:;]\s*\K\S+

Regex demo | Python demo

import regex

pattern = r"(?i)\bUsername\s*[:;]\s*\K\S+"

test_str = ("Username:dasdsad\n"
            "username ;dasdsad\n"
            "username : dasdsad\n"
            "username; dasdsad")

print(regex.sub(pattern, "REPLACEMENT", test_str))

Output

Username:REPLACEMENT
username ;REPLACEMENT
username : REPLACEMENT
username; REPLACEMENT

Upvotes: 2

Related Questions