I'm trying to match a pattern in a url that does not include a number.
For example:
/painters/1-joe-bob/dashboard
I would only want to match urls that are the following:
/painters
/painters/string
If the url includes /painters/1-something then there should be no match.
I've been trying the following with no luck:
\/{1}(painters|contractors)\/?[^0-9][a-z]*
This still matches on /painters/ or /contractors/
Please advise.
You can try this regex. It uses a negative lookahead to disallow a match if a number comes after your second forward slash.
^\/(painters|contractors)\/(?![0-9])
Note that if you don't want number anywhere in the string you can use a negative lookahead right at the beginning.
^(?!.*[0-9])\/(painters|contractors)\/
This construct will disallow any string containing numbers.
Related
I want to replace a pattern in Ruby only if the next letter after the pattern is one of the given.
Example: replace "αυ" with "av" ONLY IF next letter after "αυ" is one of the followings: α|γ|δ|λ|μ|ν|ρ|σμ|ω
This code will not work of course, I suppose I need to use a regex more complicate to match one of the letter after the pattern.
string.gsub!("αυ", "av") if string =~ /α|γ|δ|λ|μ|ν|ρ|σμ|ω/
Thanks for any suggestion.
Use a positive lookahead:
string.gsub!(/αυ(?=α|γ|δ|λ|μ|ν|ρ|σμ|ω)/, "av")
See the Rubular demo
Details
αυ - a αυ substring
(?=α|γ|δ|λ|μ|ν|ρ|σμ|ω) - a positive lookahead that requires the presence of one of the alternatives inside it while excluding the alternative inside the match value, i.e. it will be left in the resulting string).
You may also "contract" the single-char alternations into a character class
/αυ(?=[αγδλμνρω]|σμ)/
^^^^^^^^^^
See another Rubular demo. σμ cannot be put inside a character class since it contains 2 chars.
Given the following string:
details.html?id=8220&inr=4241&marke=Ford&modell=Focus&art=Gebrauchtwagen&standort=
I need to match 82204241 in a single expression. I need to extract all numbers from it as a single match. Any idea how this can be solved?
(\d+) will create two matches. I also tried with something like this without any luck: details\.html\?[id=|.*inr=]+(\d+)
Regex only matches a substring of the original string. Since 82204241 does not appear as a substring in the original string, it is impossible to match that as a single match with a regex.
How about joining regex scan? Here:
a = "details.html?id=8220&inr=4241&marke=Ford&modell=Focus&art=Gebrauchtwagen&standort="
a.scan(/\d+/).join
# => "82204241"
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)
How can I write a regex in Ruby 1.9.2 that will determine if a string meets this criteria:
Can only include letters, numbers and the - character
Cannot be an empty string, i.e. cannot have a length of 0
Must contain at least one letter
/\A[a-z0-9-]*[a-z][a-z0-9-]*\z/i
It goes like
beginning of string
some (or zero) letters, digits and/or dashes
a letter
some (or zero) letters, digits and/or dashes
end of string
I suppose these two will help you: /\A[a-z0-9\-]{1,}\z/i and /[a-z]{1,}/i. The first one checks on first two rules and the second one checks for the last condition.
No regex:
str.count("a-zA-Z") > 0 && str.count("^a-zA-Z0-9-") == 0
You can take a look at this tutorial for how to use regular expressions in ruby. With regards to what you need, you can use the following:
^[A-Za-z0-9\-]+$
The ^ will instruct the regex engine to start matching from the very beginning of the string.
The [..] will instruct the regex engine to match any one of the characters they contain.
A-Z mean any upper case letter, a-z means any lower case letter and 0-9 means any number.
The \- will instruct the regex engine to match the -. The \ is used infront of it because the - in regex is a special symbol, so it needs to be escaped
The $ will instruct the regex engine to stop matching at the end of the line.
The + instructs the regex engine to match what is contained between the square brackets one or more time.
You can also use the \i flag to make your search case insensitive, so the regex might become something like this:
^[a-z0-9\-]+/i$
I was wondering if it was possible to use negative matching on whole words, so that something like [^(<em>.*?<\/em>)] would match everything but text between (and including) <em>...</em>.
I was thinking about using negative lookahead, but I don't think this will work, as I need to check for the opening <em> as well.
Of course, I could just use the positive regex and then subtract the matches from the original text, but I'm looking for a more 'elegant' solution.
thx for any help
String#split works as negative match. It returns you an array of whatever part that does not match the regex.
'XXXXXXXX<em>YYYYYYY</em>ZZZZZZZZ'.split(%r|<em>.*?</em>|)
# => ['XXXXXXX', 'ZZZZZZZZ']
And if want it back into a string, just do join.
'XXXXXXXX<em>YYYYYYY</em>ZZZZZZZZ'.split(%r|<em>.*?</em>|).join
# => 'XXXXXXXZZZZZZZZ'
The whole thing with lookaround is that it doesn't consume any of the input. If you want to match everything but a pattern, it means that you want to match the prefix and the suffix of that pattern. To match the suffix, you probably want to consume --- and throw away -- the pattern that you don't want. But negative lookahead doesn't consume.