Shar
Shar

Reputation: 507

Match words not beginning with certain character

I am trying to mark and replace words that do not start with # from a text. The text file looks like this:

Some words #word #anotherword #etc
Some more words #words #anotherword #etc #etc more words here
No words containing that character in this line
Etc

What should be matched:

Some words 
Some more words                           more words here
No words containing that character in this line
Etc

I am totally new to regex and have been trying to come up with the right code. The closest i got is marking all the text excluding the # only, not the word attached to it as well. The part after the # shouldn't be selected. Note: special characters and numbers also exist and should be marked, but not the ones after the #. How do i accomplish this the simplest way?

Upvotes: 1

Views: 226

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

You may use

(?<!\S)[^\s#]\S*

Details

  • (?<!\S) - whitespace char or start of string must come right before the current location
  • [^\s#] - any char but whitespace and #
  • \S* - any 0+ non-whitespace chars.

See the regex demo.

Upvotes: 1

Related Questions