I have to create a regex that finds next strings:
stackoverflow //not found
stackexchange //found 'stackexchange'
stacksomething //found 'stacksomething '
stacksomething another words //find 'stacksomething '
Explanation: find a string that:
starts with 'stack'
ends with any word except 'overflow'
find only this word.
I created regex which corresponds to the first point, but can't implement the second and the third. I tried solutions like ^((?!overflow).)*$ and ^(?!.*\boverflow\b) but they don't work. That's what I have:
stack.*
You can use this negative lookahead regex:
\bstack(?!\w*overflow\b)\w*
RegEx Demo
Breakup:
\b # word boundary
stack # match literal text stack
(?!\w*overflow\b) # negative lookahead to fail the match if word ends with overflow
\w* # match 0 or more word characters to get full word match
Related
Hello good afternoon!!
I'm new to the world of regular expressions and would like some help creating the following expression!
I have a query that returns the following values:
caixa-pod
config-pod
consultas-pod
entregas-pod
monitoramento-pod
vendas-pod
I would like the results to be presented as follows:
caixa
config
consultas
entregas
monitoramento
vendas
In this case, it would exclude the word "-pod" from each value.
I would try (.*)-pod. It is not clear, where do you want to use that regexp (so regexp can be different). I guess it is dashboard variable.
You can try
\b[a-z]*(?=-pod)\b
This regex basically tells the regex engine to match
\b a word boundary
[a-z]* any number of lowercase characters in range a-z (feel free to extend to whatever is needed e.g. [a-zA-Z0-9] matches all alphanumeric characters)
(?=-pod) followed by -pod but exclude that from the result (positive lookahead)
\b another word boundary
\b matches a word boundary position between a word character and non-word character or position (start / end of string).
i'm trying to grab a regex from source, but only name from this type.
"name":"HELP-PERP","posOnly":false,"price":40.3,"priceIncrement":0.01,"quote":null,"quoteV":73851918.483,"restricted":false,"sizeIncrement":0.01,"type":"future",
So i got here \b(\w*-PERP\w*)\b
This grabs the word HELP-PERP but duplicates it, so i'm trying to grab that word that matches the type =future.
Grab help-perp that is in the same line with type":"future".
Total nub at this, i've tried several things on regex101 and can't come up :(
Thank you
You can use
/\w*-PERP\w*\b(?=.*type":"future")/g
See the regex demo.
Details
\w*-PERP\w* - zero or more word chars, -PERP, and again zero or more chars
\b - a word boundary
(?=.*type":"future") - a positive lookahead that matches a location in string that is immediately followed with any zero or more chars other than line break chars as many as possible (.*) and then a type":"future" string.
This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 5 years ago.
I want to match any word that starts/ends or not contain with word "end" but ignore word "end", for example:
hello - would match
boyfriend - would match
endless - would match
endend - would match
but
end - would NOT match
I'm using ^(?!end)).*$ but its not what I want.
Sorry for my english
Try this:
^(?!(end)$).+$
This will match everything except end.
You can use this \b(?!(?:end\b))[\w]+
Components:
\b -> Start of the word boundary for each words.
(?! Negative lookahead to eliminate the word end.
(?:end\b) Non capturing parenthesis with the word end and word boundary.
) Closing tag for negative lookahead.
[\w]+ character class to capture words.
Explanation: The regex search will only look for locations starting with word boundaries, and will remove matches with end as only word. i.e [WORD BOUNDARY]end[END OF WORD BOUNDARY]. \w will capture rest of the word. You can keep incrementing this character class if you wish to capture some special characters like $ etc.
So you want to match any word, but not "end" ?
Unless I'm misunderstanding, a conditional statement is everything that is needed... In pseudocode:
if (word != "end") {
// Match
}
If you want to match all the words in a text that are not "end" you could just remove all the non-alpha characters, replace pattern (^end | end | end$) by an empty string, and then do a string split.
The other answers with a single regex might be better then, because regex matches are O(n), no matter of the pattern.
For example,
MR-L6LQN-LP MR-L6LQN-LQ Way
should return MR-L6LQN-LQ here the specific word is 'Way'
if the string is
MR-L6LQN-LP MR-L6LQN-LQ
output should be
MR-L6LQN-LQ
I think lookahead ideas would come in handy but am not able to work it out. Please suggest regex only solution.
You can use this regex with a positive lookahead:
\b[A-Z0-9-]+(?=\s+Way|$)
RegEx Demo
RegEx Breakup:
\b: Assert word boundary
[A-Z0-9-]+: Match one or more of uppercase letters, digits or hyphens
(?=\s+Way|$): Positive Lookahead to assert we have spaces and Way ahead or end of line.
I want to match all words except following words :
1) any-random-word
2) any-random-word/
3) any-random-word/123
4) any-random-word/abcdef
so that following similar words can be matched.
1) any-random-word123
2) any-random-word(any non-word character other than '/')123
2) any-random-wordabcdef
4) any-random-word(any non-word character other than '/')abcdef
In fact any number or any word can be appened after 'any-random-word/'.
I tried with
^(?!any-random-word(\/?)(\w+)$|any-random-word$)
but its escaping all words having any-random-word in it.
Thanks.
You can change your current regex a little:
^(?!.*\bany-random-word\b)
And if you want to actually match something, add .+ at the end:
^(?!.*\bany-random-word\b).+
regex101 demo
\b (a word boundary) ensures that there's no other \w character around the word you don't want to match.
Edit: As per your further clarification, I would suggest this regex:
^(?!.*\bany-random-word(?:/|$)).+
The main part of the regex is the negative lookahead: (?!.*\bany-random-word(?:/|$)). It will cause the whole match to fail if what's inside matches.
.*\bany-random-word(?:/|$) will match any-random-word at the end of a string or followed by /, anywhere in the string that it is being tested against.
So, if you have any-random-word/, it will match, and cause the whole match to fail. If you have the string ending in any-random-word, again, the whole match will fail.