1

How could I find all instances of a substring in a string?

For example I have the string ("%1 is going to the %2 with %3"). I need to extract all placeholders in this string (%1, %2, %3)

The current code could only find the first two because the ending is not a white space.

import re
string = "%1 is going to the %2 with %3"


r = re.compile('%(.*?) ')
m = r.finditer(string)
for y in m:
 print (y.group())

1 Answer 1

5

Don't match on whitespace, match on a word boundary instead using \b:

r = re.compile(r'%(.*?)\b')

You may want to restrict your characters to word characters only instead of the . wildcard, and match at least one character:

r = re.compile(r'%(\w+)\b')

You don't appear to be using the capturing group either, so you could just omit that:

r = re.compile(r'%\w+\b')
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Martijn, what if the placeholder was to change from %1 to %1%, how could I extract that entire substring?
Then you can just match on the second %: r'%(\w+)%'.
What if that is an unknown character, it could be a %, $ or even %%?
Only word-constituents would pose a problem (matching \w) for obvious reasons.
@user1322582: what are you trying to do? You can create a dynamic regular expression, or you can use a character class to match the possible characters: r'(?P<meta>(%|%%|$|$$)\w+(?P=meta)' would match one of 4 different meta characters, provided that the same meta character(s) are used after the name as well.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.