简体   繁体   中英

Regex: exclude lines beginning with whitespace

I want to match any line starting with some arbitrary number of asterisks, followed by a whitespace and some text. For example:

** some text  

This is what I'm using:

/(\A\**)\ (.*)/

That works fine, except for the case where the line begins with a whitespace. As you can see in this example at rubular , the whitespace is the first match group and the asterisks and text are the second. All my attempts to fix it have ended up matching other, non-asterisk characters. What's the proper way to do this?

Edit: I wasn't clear: I want the match fail if the line starts with whitespace.

如果您的目标行需要以*开头的可选空格开头,那么您应该使用+ (表示一个或多个)而不是* (表示零或更多)与可选空格[ ]*

^[ ]*[*]+ (.*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM