I want to get a repetitive string in this example with regex (python):
#txt1#txt2#txt3#txt4
I tested with this pattern:
\#(.*?)
but don't work Thank you
A lazy dot pattern .*? at the end of a pattern always matches an empty string because .*? matches as few as possible occurrences of the quantified pattern, and since it can match 0 chars, it matches 0 chars.
For strings having # delimited values, you may use a negated character class [^#] with a * quantifier:
import re
s = '#txt1#txt2#txt3#txt4'
print(re.findall(r"#([^#]*)", s))
# => ['txt1', 'txt2', 'txt3', 'txt4']
See the Python demo.
The #([^#]*) pattern matches a # and then matches and captures into Group 1 any 0+ characters other than #. re.findall finds all non-overlapping occurrences of the pattern and only returns the values captured into Group 1.
NOTE: To make sure you do not get empty values in the result, you should replace the * quantifier with a + one that matches 1 or more occurrences.
In this case, you should choose a splitting approach. In case you have just a hard-coded delimiter, like #|, all you need is str.split():
s = '#|txt1#|txt2#|txt3#|txt4'
res = filter(None, s.split('#|'))
print(res)
# => ['txt1', 'txt2', 'txt3', 'txt4']
See another Python demo. Note that filter(None, res) will remove all empty strings from the res.
If you have a delimiter that is not hard-coded, you may use a re.split.
'[^#]+'#. It seems reasonable to use a negated character class in this scenario. There are other possible ways to match it, splitting with # will also work here, but I am focusing on why the original regex does not work and explain what the regex way is to solve it.#| symbols?
'#txt1#txt2#txt3#txt4'.strip('#').split('#')text0#text1#text2andtext0is not an expected value in the result. Or is a part of a larger regex pattern.