1

After looking a few similar questions, I have not been able to successfully implement a substring split on my data. For my specific case, I have a bunch of strings, and each string has a substring I need to extract. The strings are grouped together in a list and my data is NBA positions. I need to pull out the positions (either 'PG', 'SG', 'SF', 'PF', or 'C') from each string. Some strings will have more than one position. Here is the data.

text = ['Chi\xa0SG, SF\xa0\xa0DTD','Cle\xa0PF']

The code should ideally look at the first string, 'Chi\xa0SG, SF\xa0\xa0DTD', and return ['SG','SF'] the two positions. The code should look at the second string and return ['PF'].

1
  • 1
    can you add complete expected output for clarity? for ex: is this what you are looking for? [re.findall(r'\b(PG|SG|SF|PF|C)\b', s) for s in text] Commented Oct 17, 2016 at 4:55

2 Answers 2

2

Leverage (zero width) lookarounds:

(?<!\w)PG|SG|SF|PF|C(?!\w)
  • (?<!\w) is zero width negative lookbehind pattern, making sure the desired match is not preceded by any alphanumerics

  • PG|SG|SF|PF|C matches any of the desired patterns

  • (?!\w) is zero width negative lookahead pattern making sure the match is not followed by any alphanumerics

Example:

In [7]: s = 'Chi\xa0SG, SF\xa0\xa0DTD'

In [8]: re.findall(r'(?<!\w)PG|SG|SF|PF|C(?!\w)', s)
Out[8]: ['SG', 'SF']
Sign up to request clarification or add additional context in comments.

1 Comment

why not use word boundary? r'\b(PG|SG|SF|PF|C)\b'
0

heemayl's response is the most correct, but you could probably get away with splitting on commas and keeping only the last two (or in the case of 'C', the last) characters in each substring.

s = 'Chi\xa0SG, SF\xa0\xa0DTD'
fin = list(map(lambda x: x[-2:] if x != 'C' else x[-1:],s.split(',')))

I can't test this at the moment as I'm on a chromebook but it should work.

1 Comment

I currently have no way to test it but I gave it a go anyway

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.