Exclude any word followed by # from regex - regex

I'm trying to use the regex.replace function in VB.NET, and I want to exclude any word that has an # symbol after it. At the moment, the pattern I'm using is "/b" & Term & "/b" (where Term is whatever word I want to replace).
Thanks.

You may try this:
\b(?<!#)[^#\s]+(?!#)\b
Regex Demo
Explanation
[^#\s]+ This will exclude any word that has '#'within or just
after it. character class [^] that starts with ^ indicates negate anything that is within the character class. Thus, ^ inside [] doesn't mean start of a string.
In many flavor The word boundary \b includes # as a boundary value.
Therefore you need to make sure that \b doesn't consider # as a
boundary. Therefore the lookahead and lookbehind has been introduced
here.
The first \b(?<!#) ensures word boundary but not #
The last (?!#)\b ensures word boundy but not #

Related

Regex help to exclude [duplicate]

What is the regular expression to search for word string that is not followed by the # symbol?
For example:
mywordLLD OK
myword.dff OK
myword#ld Exclude
The (?!#) negative look-ahead will make word match only if # does not appear immediately after word:
word(?!#)
If you need to fail a match when a word is followed with a character/string somewhere to the right, you may use any of the three below
word(?!.*#) # Note this will require # to be on the same line as word
(?s)word(?!.*#) # (except Ruby, where you need (?m)): This will check for # anywhere...
word(?![\s\S]*#) # ... after word even if it is on the next line(s)
See demo
This regex matches word substring and (?!#) makes sure there is no # right after it, and if it is there, the word is not returned as a match (i.e. the match fails).
From Regular-expressions.info:
Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, this tutorial explained why you cannot use a negated character class to match a q not followed by a u. Negative lookahead provides the solution: q(?!u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point.
And on Character classes page:
It is important to remember that a negated character class still must match a character. q[^u] does not mean: "a q not followed by a u". It means: "a q followed by a character that is not a u". It does not match the q in the string Iraq. It does match the q and the space after the q in Iraq is a country. Indeed: the space becomes part of the overall match, because it is the "character that is not a u" that is matched by the negated character class in the above regexp. If you want the regex to match the q, and only the q, in both strings, you need to use negative lookahead: q(?!u).

What is the correct way to do a search in vscode with regex for NOT the letter 's' [duplicate]

What is the regular expression to search for word string that is not followed by the # symbol?
For example:
mywordLLD OK
myword.dff OK
myword#ld Exclude
The (?!#) negative look-ahead will make word match only if # does not appear immediately after word:
word(?!#)
If you need to fail a match when a word is followed with a character/string somewhere to the right, you may use any of the three below
word(?!.*#) # Note this will require # to be on the same line as word
(?s)word(?!.*#) # (except Ruby, where you need (?m)): This will check for # anywhere...
word(?![\s\S]*#) # ... after word even if it is on the next line(s)
See demo
This regex matches word substring and (?!#) makes sure there is no # right after it, and if it is there, the word is not returned as a match (i.e. the match fails).
From Regular-expressions.info:
Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, this tutorial explained why you cannot use a negated character class to match a q not followed by a u. Negative lookahead provides the solution: q(?!u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point.
And on Character classes page:
It is important to remember that a negated character class still must match a character. q[^u] does not mean: "a q not followed by a u". It means: "a q followed by a character that is not a u". It does not match the q in the string Iraq. It does match the q and the space after the q in Iraq is a country. Indeed: the space becomes part of the overall match, because it is the "character that is not a u" that is matched by the negated character class in the above regexp. If you want the regex to match the q, and only the q, in both strings, you need to use negative lookahead: q(?!u).

How to build RegEx which skip any braces '[' characters inside a word or just hit last character? [duplicate]

What is the regular expression to search for word string that is not followed by the # symbol?
For example:
mywordLLD OK
myword.dff OK
myword#ld Exclude
The (?!#) negative look-ahead will make word match only if # does not appear immediately after word:
word(?!#)
If you need to fail a match when a word is followed with a character/string somewhere to the right, you may use any of the three below
word(?!.*#) # Note this will require # to be on the same line as word
(?s)word(?!.*#) # (except Ruby, where you need (?m)): This will check for # anywhere...
word(?![\s\S]*#) # ... after word even if it is on the next line(s)
See demo
This regex matches word substring and (?!#) makes sure there is no # right after it, and if it is there, the word is not returned as a match (i.e. the match fails).
From Regular-expressions.info:
Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, this tutorial explained why you cannot use a negated character class to match a q not followed by a u. Negative lookahead provides the solution: q(?!u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point.
And on Character classes page:
It is important to remember that a negated character class still must match a character. q[^u] does not mean: "a q not followed by a u". It means: "a q followed by a character that is not a u". It does not match the q in the string Iraq. It does match the q and the space after the q in Iraq is a country. Indeed: the space becomes part of the overall match, because it is the "character that is not a u" that is matched by the negated character class in the above regexp. If you want the regex to match the q, and only the q, in both strings, you need to use negative lookahead: q(?!u).

Matching Regex with # character

I am trying to match the following string:
style #
My regex is as follows:
^\s*\b(style #)\b\s*$
This is not matching my string.
If I try this regex:
^\s*\b(style n)\b\s*$
It matches the following string:
style n
This leads me to think that I am using the # character incorrectly.
What am I doing wrong?
The problem is that \b means a word boundary (with a letter/number/underscore on exactly one side), and your string doesn't have a word boundary after the # (because it's not followed by a letter/number/underscore). Just drop that part.
^\s*\b(style #)\s*$
(And you actually don't need the first \b, either, since the context guarantees there'll be a word boundary there.)

Regex - how to exclude single word?

I am using http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/ for validation. Validation rules are defined in a following way:
"onlyLetterSp": {
"regex": /^[a-zA-Z\ \']+$/,
"alertText": "* Only letters"
}
I would like to add new rule, which will exclude one single word. I have read some similar questions on StackOverflow and tried to declare it with something like this
"regex": /(?!exclude_word)\^[a-zA-Z\ \']+$/,
But it didn't work. Can you give me some advices how to do it?
This is a good time to use word boundary assertions, like #FailedDev indicated, but care needs to be exercised to avoid rejecting certain not-TOO-special cases, such as wordy, wordsmith or even not so obviously cases like sword or foreword
I believe this will work pretty well:
\b(?!\bword\b)\w+\b
This is the expression broken down:
\b # assert at a word boundary
(?! # look ahead and assert that what follows IS NOT...
\b # a word boundary
word # followed by the exact characters `word`
\b # followed by a word boundary
) # end look-ahead assertion
\w+ # match one or more word characters: `[a-zA-Z0-9_]`
\b # then a word boundary
The expression in the original question, however, matches more than word characters. [a-zA-Z\ \']+ matches spaces (to support multiple words in the input) and single quotes as well (for apostrophes?). If you need to allow words with apostrophes in them then use the following expression:
\b(?!\bword\b)[a-zA-Z']+\b
\b(?:(?!word)\w)+\b
Will not match the "word".
It's unclear from your question what you want, but I've interpreted it as "not matching input that contains a particular word". The regex for this is:
^(?!.*\bexclude_word\b)