regex - How do I match a certain string that does not contain an asterisk? -


i have regex captures keyword comes after number sign:

/^#\s*([a-za-z\-\s]+)/ 

however, need change regex specify not match string contains asterisk. instance, need regex match # keyword, not # *keyword.

the following best attempt @ solving this:

/^#\s[^[*]]*([a-za-z\-\s]+)/ 

i'm brand new perl i'm sure solution simple, time spent researching , trial , error didn't me whole lot.

assuming i'm understanding correctly, first regex fine. whitelist , asterisk character isn't in there, won't match keyword containing asterisk character:

/^#\s*([a-za-z\-\s]+)/ 

this still match like:

# key*word 

... although key matched. 1 solution, if sure keyword take rest of line, force whitelisted characters appear until end of line, so:

/^#\s*([a-za-z\-\s]+)$/ 

here's option. if want make sure first set of characters after initial hash , whitespace doesn't contain asterisk, can use:

/^#\s*([a-za-z\-]+)(?:\s|$)/ 

this match:

# keyword foo 

and match:

# keyword 

but not match:

# key*word foo 

nor:

# key*word 

nor:

# key* word foo 

nor:

# **** keyword 

Comments