Suppose I have string variables like following:
s1="10$"
s2="10$ I am a student"
s3="10$Good"
s4="10$ Nice weekend!"
As you see above, s2 and s4 have white space(s) after 10$ .
Generally, I would like to have a way to check if a string start with 10$ and have white-space(s) after 10$ . For example, The rule should find s2 and s4 in my above case. how to define such rule to check if a string start with ’10$’ and have white space(s) after?
What I mean is something like s2.RULE? should return true or false to tell if it is the matched string.
———- update ——————-
please also tell the solution if 10# is used instead of 10$
You can do this using Regular Expressions (Ruby has Perl-style regular expressions, to be exact).
The regular expression breaks down like this:
/at the beginning and the end tell Ruby that everything in between is part of the regular expression\Amatches the beginning of a string10is matched verbatim\$means to match a$verbatim. We need to escape it since$has a special meaning in regular expressions.[ \t]+means “match at least one blank and/or tab”So this regular expressions says “Match every string that starts with
10$followed by at least one blank or tab character”. Using the=~you can test strings in Ruby against this expression.=~will return a non-nil value, which evaluates to true if used in a conditional likeif.Edit: Updated white space matching as per Asmageddon’s suggestion.