Zombie Freecss
Zombie Freecss

Reputation: 21

Negation of a regex

I am trying to negate a regex. I have a sentence, say, "Do you #know this, please #help". By using the regex #\w*, I can extract the words: #know and #help. What is the regex to be used to extract remaining words? Something like a negation of #\w*. I am using regex option in Rapid Miner tool and thus I do not have other functions of any programming language.

Upvotes: 0

Views: 122

Answers (4)

user557597
user557597

Reputation:

This gets the remaining words (?<![\w#])\w+

https://regex101.com/r/QXkg14/1

Upvotes: 0

aquinas
aquinas

Reputation: 23786

Try: \b(?<!#)(\w+)

Demo

Match a word boundary but only if the first character in the word is NOT a # and then match word characters.

Upvotes: 2

Alp
Alp

Reputation: 3105

I am not very clear what you are asking for. However, I came up with the following fiddle. Please check this out

Here is what I did:

((?<=#)\w*)|(?!#)\w*
   |    |     |   +--> A word  
   |    |     +------> Only match the above word if # is not in front of it
   |    +------------> A word
   +-----------------> Only match the above word if there is # in front of it

(First one is a positive lookbehind and the second expression is a negative lookbehind.)

I combined these two conditions with a pipe(|) but you don't need to. If you need matching words separately, you need to do 2 sets of regex expressions.

Upvotes: 0

user6486124
user6486124

Reputation:

I cannot give you a solution here, but I can recommend you a good website to create your very own regular expression. Since I got to know, that the regex used here is based on the perl syntax, you could make use of this Regular Expression Generator to solve your problem. Just remember to set "Perl" as your flavor for your expression. Best of luck!

Regex101

Upvotes: 0

Related Questions