Exclude beginning string from regex - ruby

I have a piece of regex that looks like this:
/^.*website.localdev$/
This matches website.localdev as well as any subdomain like www.website.localdev fine.
I need to adapt it to exclude a string ("foo") at the beginning of the regex, so that "website.localdev" and "www.website.localdev" still matches but "foo.website.localdev" does not.

You should use negative lookahead:
/^(?!foo\.).*?website.localdev$/
You can see it in action on rubular

Related

How to check for link regex in text with RSpec?

I have a string of html that looks like this:
Click the link below:
Verify
I would like to check for the presence of the link, but substitute wildcards for the params that I don't care about. I've tried this but it doesn't match:
expect(html).to match %r{Verify}
How can I get this to work?
You need to escape special characters that could be interpreted as regex matchers. Here the ?
expect(html).to match %r{Verify}

Ruby gsub/regex to find all chars but not specific words

My Objective:
I have a string like so:
"O_1324||T_6789||EC_67889&&(IC_12345||chicken)||true&&false"
My dream is to use a gsub regex to identify [a-zA-z0-9_] and replace them with something ("false" if you must know). However I don't want to replace the words "true" or "false".
What have I tried
I have been using the super friendly Rubular with little success.
I can get all the "words" (non operators) like so:
(\w+)
I tried matching all the "words" except "true" like so:
(?!true)(\w+)
This did not work. It unmatches only the "t" in true.
You can use following regex :
\b(?:(?!true|false)\b)\w+\b
see demo https://regex101.com/r/eX6rE6/1
Note that you need to use word boundary for matching words. and put the negative look-ahead before \w+ not after!

gsub same pattern from a string

I have big problems with figuring out how regex works.
I want this text:
This is an example\e[213] text\e[123] for demonstration
to become this:
This is an example text for demonstration.
So this means that I want to remove all strings that begin with \e[ and end with ]
I just cant find a proper regex for this.
My current regex looks like this:
/.*?(\\e\[.*\])?.*/ig
But it dont work. I appreciate every help.
You only need to do this:
txt.gsub(/\\e\[[^\]]*\]/i, "")
There is no need to match what is before or after with .*
The second problem is that you use .* to describe the content between brackets. Since the * quantifier is by default greedy, it will match all until the last closing bracket in the same line.
To prevent this behaviour a way is to use a negated character class in place of the dot that excludes the closing square brackets [^\]]. In this way you keep the advantage of using a greedy quantifier.
gsub can do the global matching for you.
re = /\\e\[.+?\]/i
'This is an example\e[213] text\e[123] for demonstration'.gsub re, ''
=> "This is an example text for demonstration"
You can make the search less greedy by using .+? in the regex
puts 'This is an example\e[213] text\e[123] for demonstration'.gsub(/\\e\[.+?\]/, '')
This is an example text for demonstration
=> nil

ruby regex match any character besides a specific one

I am looking for a way to match any character besides, for example, a "#."
It would look something like...
gsub(/^foo.*foo$/)
But I'd want it to match
"foofdfdfdfoo"
But not
"fooddgdgd#fdfoo"
Thanks.
^[^#]+$
http://rubular.com/r/glijo99dU9
gsub is for substitution. If you just want to match, the .match method
To expand on Explosion Pills answer, a caret (^) will negate the match in a regex. This means that it will not match if the characters following it are found in the expression. You can read more about it in the documentation.

How to match anything EXCEPT this string?

How can I match a string that is NOT partners?
Here is what I have that matches partners:
/^partners$/i
I've tried the following to NOT match partners but doesn't seem to work:
/^(?!partners)$/i
Your regex
/^(?!partners)$/i
only matches empty lines because you didn't include the end-of-line anchor in your lookahead assertion. Lookaheads do just that - they "look ahead" without actually matching any characters, so only lines that match the regex ^$ will succeed.
This would work:
/^(?!partners$)/i
This reports a match with any string (or, since we're in Ruby here, any line in a multi-line string) that's different from partners. Note that it only matches the empty string at the start of the line. Which is enough for validation purposes, but the match result will be "" (instead of nil which you'd get if the match failed entirely).
not easily but with the look ahead operator it can.
Here the ruby regex
^((?!partners).)*$
Cheers
If you only want to get a true value when string is not partners then there is no need to use regex and you can just use a string comparison (which ignores case).
If you for some reason need a positive regex match for any string which does not contain partners (if it's a part of a larger regex for example) you could use several different constructs, like:
`^(?:(?!partners).)*$`
or
^(?:[^p]+|p(?!artners))*$
For example, in Java:
!"partners".equalsIgnoreCase(aString)

Resources