shuberman
shuberman

Reputation: 1490

Regular expression not excluding special character hyphen (-)

I have a long text where I am supposed to search for a 16 character word that starts with c

I wrote a regex as follows:

c[^\s\—][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-][^\s\-]

But,

it matches below 2 words

commodity—anythi
circumnavigation

I am learning regex but from what I know so far, carat^ character should exclude hyphen- from my results, so I am unable to explain how commodity—anythi is showing up in my results?

I would appreciate if someone can explain me this behaviour. Thank you.

Upvotes: 0

Views: 33

Answers (1)

Jan
Jan

Reputation: 43169

As stated in the comments, you have two characters here, a minus sign and a dash: is not equal to -. Additionally, consider using quantifiers:

c[^\s\—]+

See the comparison on regex101.com.

Upvotes: 1

Related Questions