2

I have a string that I am trying to split into 2 strings using Regex to form a list. Below is the string:

Input: 'TLSD_IBPDEq.'

Output: ['', '']

Expected Output: ['TLSD_IBPD', 'Eq.']

Below is what I have tried but is not working

pattern = r"\S*Eq[\.,]"
l = re.split(pattern,"TLSD_IBPDEq.")

print(l)  => ['', '']
5
  • What exactly is this supposed to do? Can you explain what you want the RegEx to do? Commented Nov 23, 2022 at 0:23
  • 1
    Do you want only match string that ends with Eq. and then make 2-item list? Commented Nov 23, 2022 at 0:24
  • pattern = r'Eq\.' Commented Nov 23, 2022 at 0:26
  • I want to split input string which in this case is "TLSD_IBPDEq." into 2 based on a match which is "Eq." Commented Nov 23, 2022 at 0:26
  • Yes I only want to match string that ends with "Eq." and make them into 2 item list Commented Nov 23, 2022 at 0:29

2 Answers 2

1

If I understand, then you can apply the answer from this question. If you need to use a regex to solve this, then use a capture group and remove the last (empty) element, like this:

pattern = r"(Eq\.)$"
l = re.split(pattern, "TLSD_IBPDEq.")[:-1]
print(l)  # => ['TLSD_IBPD', 'Eq.']
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it without re:

s = "TLSD_IBPDEq."

if s.endswith(("Eq.", "Eq,")):
    print([s[:-3], s[-3:]])

Prints:

['TLSD_IBPD', 'Eq.']

Solution with re:

import re

s = "TLSD_IBPDEq."

print(list(re.search(r"(\S*)(Eq[.,])$", s).groups()))

Prints:

['TLSD_IBPD', 'Eq.']

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.