Returns true if path
matches against pattern
. The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:
*
Matches any file. Can be restricted by other values in the glob. Equivalent to / .* /x
in regexp.
*
Matches all files regular files
c*
Matches all files beginning with c
*c
Matches all files ending with c
*c*
Matches all files that have c
in them (including at the beginning or end).
To match hidden files (that start with a .
set the File::FNM_DOTMATCH flag.
**
Matches directories recursively or files expansively.
?
Matches any one character. Equivalent to /.{1}/
in regexp.
[set]
Matches any one character in set
. Behaves exactly like character sets in Regexp
, including set negation ([^a-z]
).
\
Escapes the next metacharacter.
{a,b}
Matches pattern a and pattern b if File::FNM_EXTGLOB flag is enabled. Behaves like a Regexp
union ((?:a|b)
).
flags
is a bitwise OR of the FNM_XXX
constants. The same glob pattern and flags are used by Dir::glob
.
Examples:
File.fnmatch('cat', 'cat') #=> true # match entire string File.fnmatch('cat', 'category') #=> false # only match partial string File.fnmatch('c{at,ub}s', 'cats') #=> false # { } isn't supported by default File.fnmatch('c{at,ub}s', 'cats', File::FNM_EXTGLOB) #=> true # { } is supported on FNM_EXTGLOB File.fnmatch('c?t', 'cat') #=> true # '?' match only 1 character File.fnmatch('c??t', 'cat') #=> false # ditto File.fnmatch('c*', 'cats') #=> true # '*' match 0 or more characters File.fnmatch('c*t', 'c/a/b/t') #=> true # ditto File.fnmatch('ca[a-z]', 'cat') #=> true # inclusive bracket expression File.fnmatch('ca[^t]', 'cat') #=> false # exclusive bracket expression ('^' or '!') File.fnmatch('cat', 'CAT') #=> false # case sensitive File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true # case insensitive File.fnmatch('?', '/', File::FNM_PATHNAME) #=> false # wildcard doesn't match '/' on FNM_PATHNAME File.fnmatch('*', '/', File::FNM_PATHNAME) #=> false # ditto File.fnmatch('[/]', '/', File::FNM_PATHNAME) #=> false # ditto File.fnmatch('\?', '?') #=> true # escaped wildcard becomes ordinary File.fnmatch('\a', 'a') #=> true # escaped ordinary remains ordinary File.fnmatch('\a', '\a', File::FNM_NOESCAPE) #=> true # FNM_NOESCAPE makes '\' ordinary File.fnmatch('[\?]', '?') #=> true # can escape inside bracket expression File.fnmatch('*', '.profile') #=> false # wildcard doesn't match leading File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true # period by default. File.fnmatch('.*', '.profile') #=> true rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string. File.fnmatch(rbfiles, 'main.rb') #=> false File.fnmatch(rbfiles, './main.rb') #=> false File.fnmatch(rbfiles, 'lib/song.rb') #=> true File.fnmatch('**.rb', 'main.rb') #=> true File.fnmatch('**.rb', './main.rb') #=> false File.fnmatch('**.rb', 'lib/song.rb') #=> true File.fnmatch('*', 'dave/.profile') #=> true pattern = '*' '/' '*' File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME) #=> false File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true pattern = '**' '/' 'foo' File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME) #=> true File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME) #=> true File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME) #=> true File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME) #=> false File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
Converts pattern to a Regexp
(if it isn’t already one), then invokes its match
method on str. If the second parameter is present, it specifies the position in the string to begin the search.
'hello'.match('(.)\1') #=> #<MatchData "ll" 1:"l"> 'hello'.match('(.)\1')[0] #=> "ll" 'hello'.match(/(.)\1/)[0] #=> "ll" 'hello'.match(/(.)\1/, 3) #=> nil 'hello'.match('xx') #=> nil
If a block is given, invoke the block with MatchData
if match succeed, so that you can write
str.match(pat) {|m| ...}
instead of
if m = str.match(pat) ... end
The return value is a value from block execution in this case.
Converts pattern to a Regexp
(if it isn’t already one), then returns a true
or false
indicates whether the regexp is matched str or not without updating $~
and other related variables. If the second parameter is present, it specifies the position in the string to begin the search.
"Ruby".match?(/R.../) #=> true "Ruby".match?(/R.../, 1) #=> false "Ruby".match?(/P.../) #=> false $& #=> nil
Returns a MatchData
object describing the match, or nil
if there was no match. This is equivalent to retrieving the value of the special variable $~
following a normal match. If the second parameter is present, it specifies the position in the string to begin the search.
/(.)(.)(.)/.match("abc")[2] #=> "b" /(.)(.)/.match("abc", 1)[2] #=> "c"
If a block is given, invoke the block with MatchData
if match succeed, so that you can write
/M(.*)/.match("Matz") do |m| puts m[0] puts m[1] end
instead of
if m = /M(.*)/.match("Matz") puts m[0] puts m[1] end
The return value is a value from block execution in this case.
Returns a true
or false
indicates whether the regexp is matched or not without updating $~ and other related variables. If the second parameter is present, it specifies the position in the string to begin the search.
/R.../.match?("Ruby") #=> true /R.../.match?("Ruby", 1) #=> false /P.../.match?("Ruby") #=> false $& #=> nil
Returns sym.to_s.match
.
Returns sym.to_s.match?
.
Tests whether the given pattern
is matched from the current scan pointer. Returns the length of the match, or nil
. The scan pointer is not advanced.
s = StringScanner.new('test string') p s.match?(/\w+/) # -> 4 p s.match?(/\w+/) # -> 4 p s.match?(/\s+/) # -> nil
Returns true
iff the last match was successful.
s = StringScanner.new('test string') s.match?(/\w+/) # => 4 s.matched? # => true s.match?(/\d+/) # => nil s.matched? # => false
Returns the last matched string.
s = StringScanner.new('test string') s.match?(/\w+/) # -> 4 s.matched # -> "test"
The first form returns the MatchData
object generated by the last successful pattern match. Equivalent to reading the special global variable $~
(see Special global variables in Regexp
for details).
The second form returns the nth field in this MatchData
object. n can be a string or symbol to reference a named capture.
Note that the last_match
is local to the thread and method scope of the method that did the pattern match.
/c(.)t/ =~ 'cat' #=> 0 Regexp.last_match #=> #<MatchData "cat" 1:"a"> Regexp.last_match(0) #=> "cat" Regexp.last_match(1) #=> "a" Regexp.last_match(2) #=> nil /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ "var = val" Regexp.last_match #=> #<MatchData "var = val" lhs:"var" rhs:"val"> Regexp.last_match(:lhs) #=> "var" Regexp.last_match(:rhs) #=> "val"
Returns the size of the most recent match (see matched
), or nil
if there was no recent match.
s = StringScanner.new('test string') s.check /\w+/ # -> "test" s.matched_size # -> 4 s.check /\d+/ # -> nil s.matched_size # -> nil