I'm looking for regex or any way we can check if a string starts with bulleted text like -
1. Sample text 1
2)another sample text
iii) another sample text 3
d) another sample text 4
{E} another sample text
Is there any package or solution out there where can identify if a string starts with any style of bulleted text ?
CodePudding user response:
\d.. ||\d). ||\i ). ||[d]). ||{[E]}.
CodePudding user response:
One can check the start of the string against the unicode list of bullets. This unicode list below is incomplete.
line1 = "● Bulleted line"
line2 = "Regular line"
line3 = "1. Sample text 1"
line4 = "2)another sample text"
line5 = "iii) another sample text 3"
line6 = "d) another sample text 4"
line7 = "{E} another sample text"
unicode_bullet_lst = [u'\u2022', u'\u2023', u'\u2043', u'\u204C', u'\u204D', u'\u2219', u'\u25CB', u'\u25CF', u'\u25D8', u'\u25E6', u'\u2619', u'\u2765', u'\u2767', u'\u29BE', u'\u29BF']
pat = re.compile(r'^\d \.?\d ?|^\d \)?\.?|^\w \)|^{[\d\w] }')
def starts_with_bullet(st):
return any((st.startswith(x) or re.match(pat, st) ) for x in unicode_bullet_lst )
for l in [line1, line2, line3, line4, line5, line6, line7]:
print(f'{l} starts with a bullet? {starts_with_bullet(l)}')
● Bulleted line starts with a bullet? True
Regular line starts with a bullet? False
1. Sample text 1 starts with a bullet? True
2)another sample text starts with a bullet? True
iii) another sample text 3 starts with a bullet? True
d) another sample text 4 starts with a bullet? True
{E} another sample text starts with a bullet? True