Adding words to a character class in Regex [duplicate] - regex

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 2 years ago.
I am currently using the following character class:
[^\)\(] in my regex
I want to add the word 'hello' to this class so it is also not matched in my string.
I have tried
[^\)\((hello)]
but it does not work.
What can I do?

One typical way you would enforce that hello does not appear would be to use a negative lookahead, e.g.
^(?!.*hello)[^t()]+$
If you only wanted to exclude hello when it appears as a bona fide word, then surround it with word boundaries in the lookahead:
^(?!.*\bhello\b)[^t()]+$

Related

How to get the reversed result of the following regex? [duplicate]

This question already has answers here:
How can I "inverse match" with regex?
(10 answers)
Closed 6 months ago.
Regex: /^[0-9\p{L}.,\s]+$/u
I would like to replace the characters not matching with the regex with "".
As I understand, you simply want to drop all chars not matching your regex. So the idea is to invert the class of chars:
/^[0-9\p{L}.,\s]+$/u should become /[^\d\p{L}.,\s]+/gu (I added the ^ after the [ to say "not in this list of chars" and replaced 0-9 by \d for digits. Use the g modifier (=global ) to match multiple times.
Running it: https://regex101.com/r/IQz6K5/1
I'm not sure that ,, . and the space will be enough ponctuation. It would be interesting to have a complete example of what you are trying to achieve. You could use another unicode character class for ponctuation if needed, typically with \p{P}. See more info about unicode classes here: https://www.regular-expressions.info/unicode.html#category

How can I improve this RE to not end at "('"? [duplicate]

This question already has answers here:
regular expression for finding 'href' value of a <a> link
(9 answers)
Closed 2 years ago.
I have this regex
href=["'](.*?)["']
And I want to match this entire string but it only matches up to (' and does not include explore
href="javascript:openurl('/Xplore/accessinfo.jsp')"
It also has to match
href="/iel5/4235/4079606/04079617.pdf?tp=&arnumber=4079617&isnumber=4079606"
The first link is the only special case, I have been able to match all other cases with the regex I have provided, I just want to somehow exclude the ' in the middle of the first string.
What you could do is have a positive lookahead define the end of the string:
^href=("|').*?(?=\1)\1$
That way, no matter if its a single or double quote, the second capture group will run till it finds the same single or double quote.

Match string only when not preceded by another string [duplicate]

This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
Closed 3 years ago.
I want to match strings containing MYSTUFF in all cases, except when MYSTUFF is preceded by ENABLED_. So that the following would match:
MYSTUFF
m_MYSTUFF
MYSTUFFIsGreat
allOf_MYSTUFF
but the following wouldn't:
ENABLED_MYSTUFF
m_ENABLED_MYSTUFF
ENABLED_MYSTUFFIsGreat
allOf_ENABLED_MYSTUFF
I tried several variations using negative lookahead (variations of \w*(?!.*ENABLED_)MYSTUFF\w*), and conditional (variations of (?(?!=ENABLED_)(MYSTUFF))), but I did not manage to get the results I'm after.
Is what I want even doable with regexes?
You could accomplish this by using a negative look-behind assertion ...
\w*(?<!ENABLED_)MYSTUFF\w*
see regex demo

Selecting words in a text using regex [duplicate]

This question already has answers here:
Regex match entire words only
(7 answers)
Closed 7 years ago.
var text = 'word otherword';
I want to select words that only matches with 'word'. When i use simply /word/ for regex pattern, then it also selects the 'word' part in 'otherword'. I dont need that word(in this case 'otherword').So how can i only select/match 'word' word using regex?
Use word boundary - \b.
/\bword\b/

How To Negate Regex [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to match string not containing a word?
How can I invert a regular expression in JavaScript?
Say I have the regex foo123. How do I match everything that is not foo123?
Use negative lookahead for this.
(?!foo123).+
matches any string except foo123
If you want to match empty string also, use (?!foo123).*
In your case (according to the comment) the required regex is (?!P[0-9]{1,}).+.
It matches P and 123, but not P123.