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 following:
- hello! - hello!
Here's what I've got so far: ^(\-)
. But that doesn't match the second example above because it requires the first character to be a hyphen.
Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.
The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.
To match the start or the end of a line, we use the following anchors: Caret (^) matches the position before the first character in the string. Dollar ($) matches the position right after the last character in the string.
Start of String or Line: ^ By default, the ^ anchor specifies that the following pattern must begin at the first character position of the string. If you use ^ with the RegexOptions. Multiline option (see Regular Expression Options), the match must occur at the beginning of each line.
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)You can use this regex by making 0 or more spaces optional match at beginning:
^\s*-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With