Reputation: 19110
How do I express in regex the letter "s"
whose next non-space character is not a "/"
?
"s"
, "str"
"s/m"
, "s /n"
I tried this
"str" =~ /s[^[[:space:]]]^\// #=> nil
but it does not even match the simple use case.
Upvotes: 1
Views: 312
Reputation: 110675
If you merely want to know the number of 's'
characters that are not followed by zero or more spaces and then a forward slash (as opposed to their indices in the string), you don't have to use a regular expression.
"sea shells /by the sea s/hore".delete(" ").gsub("s/", "").count("s")
#=> 3
If you only want to know if there is at least one such 's'
you could replace count("s")
with include?("s")
.
I'm not arguing that this is preferable to the use of a regular expression.
Upvotes: 0
Reputation: 626738
It seems you need to match any s
that is not followed with any 0+ whitespace chars and a /
after them.
Use
/s(?![[:space:]]*\/)/
See the Rubular demo.
Details
s
- the letter s
(?![[:space:]]*\/)
- a negative lookahead that fails the match if, immediately to the right of the current location, there are
[[:space:]]*
- 0+ whitespaces\/
- a /
.Upvotes: 2