I am trying to write a regular expression that matches lines beginning with a hyphen (-) OR that begins with spaces or tabs and then has a hyphen. So it should match the followi
You can try
^\s*-
^
: start of string\s*
: zero or more whitespace characters-
: a literal -
(you don't need to escape this outside a character class)the above (using \s*) is the easiest one for this case, but in general, you can always use the | syntax:
re.match('^-|^\s+-', '- hello')
<_sre.SRE_Match object at 0x0000000054E72030>
re.match('^-|^\s+-', ' - hello')
<_sre.SRE_Match object at 0x0000000054E72030>
re.match('^-|^\s+-', ' + hello')
None
^-
is the case for - at beginning, `^\s+-' is with one or more spaces, and | chooses either one.
You can use this regex by making 0 or more spaces optional match at beginning:
^\s*-