1

Regex is separating into groups, not selecting first match

REGEX

([^:.]+)

DATA

DATA1:OS
DATA2-XT
DATA3.DOMAIN.COM.BR

GOAL

DATA1
DATA2-XT
DATA3

REGEX101

1
  • 2
    Add a start anchor ^ in the beginning of the regex. Commented Oct 9, 2019 at 12:56

1 Answer 1

2
import re
pattern = re.compile('[^:.]+')
str_list = ["DATA1:OS",
            "DATA2-XT",
            "DATA3.DOMAIN.COM.BR"]
for e in str_list:
    print(pattern.match(e).group())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.