I want regular expression for checking whether a word has .
or /
like abcd.
or abcd/a
or abcd.def
or abcd/def
. Any help 开发者_StackOverflow中文版will be gr8!
/[\/\.]/
Usage
if "some test. string" =~ /[\/\.]/
# ..
end
Both /
and .
are reserved characters in regex, so you would normally need to escape them. The exception to this is that .
doesn't need to be escaped when it's in a character class, which is how you'd want to search in this case.
The escape chararacter in regex is \
, so your /
character becomes \/
. Your .
remains as it is.
Therfore, to check if a string contains either a /
or a .
, you would need a regex that looks something like this:
/[.\/]/
This will check any string and return true if it contains either of these characters anywhere within the string, regardless of what else is in the string.
/(\.|\/)/
matches the period or slash to a group.
/[\/\.]/
just matches for period, slash or both.
精彩评论