Fitnesse Compare Statement - fitnesse

Is this possible to compare two strings in fitness:
!define Str {ABC}
that
${= ${Str} == ABC =}
will return true.

No, the equality operator is not supported in FitNesse expressions.

Related

Is there an equivalent of string multiplication in Lua? Eg: "o"*3

Is there an equivalent of the following code which is in python, in Lua?
print('o'*3) #=> "ooo"
The Lua equivalent of this would be...?
string.rep('o', 3)
Or:
local my_string = 'o'
my_string:rep(3)
Or:
('o'):rep(3)
(when you use this syntax, literal expressions such as 'o' must be in parentheses. 'o':rep(3) is not valid syntax)

Regular Expression: Language of all those strings which do not contain substring 'bb'

Language={a b}
RegEx of Language of all those strings which do not contain substring ‘bb’
(a+ba)*(E+b)
Remember E is epsilon means empty set
if you check this it would work
`( a + ab )*`
represents strings which do not contain any substring bb.
as it generates strings either lemda or ending on a or ab

Regex for string comparison with and without special characters

I'm trying create a regular expression for string comparison.
The regular expression is: .*\bword.*
However, I want to ignore special characters and the comparison should work with and without them.
For example:
O'Reilly should match O'Reilly and oreilly
It is possible do it with a regular expression?
P.S.
This is to be used in iOS with NSPredicate.
Currently, the predicate looks like:
NSString *regexString = [NSString stringWithFormat:#".*\b%#.*", word];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K matches[cd] %#", keypath, regexString];
Since NSPredicate doesn't allow me to do any operation like replace the value of the keypath to a value without special characters, I need to do it via regular expression.
You might think about preprocessing your string before doing the match. If you have a list of acceptable characters, which looking at your example is just a-z and A-Z you can use the transliteration operator tr/// to remove all the other characters and lc to lower case the string. The flags on tr are c compliment the match, ie match everything that is not listed and d delete everything that matched that does not have a replacement, as the replacement is empty that means everything that matched.
$string =~ tr/a-zA-Z//cd;
$string = lc $string;
If you are using characters outside the ASCII range then you need to be a little cleverer.
$string =~ s/\P{L}+//g;
$string = fc $string;
First off we use a regex to remove any Unicode character that is not in the general category letter. And then we use the fc function to fold case the string, this is the same function that Perl uses to do case insensitive regex matches. Note that you might want to normalise the string first.

Pattern ^u.meta(\.|$) not working as expected

I have this pattern:
^u.meta(\.|$)
EXPECTED BEHAVIOUR
^u.meta(\.|$) will match all the roles like:
u.meta
u.meta.admin
u.meta.admin.system
u.meta.*
Where as it should not match something like below:
u.meta_admin
u.meta_admin_system
I have tested this pattern with https://regex101.com/ online regexp tester.
PROBLEM:
I have to implement this pattern with lua script.
but getting invalid escape sequence near '\.':
-- lua script
> return string.match("u.meta.admin", '^u.meta(\.|$)')
stdin:1: invalid escape sequence near '\.'
And I tried adding double \\ as well as removing '\' escape char in that regexp but got nil in return:
-- lua script
> return string.match("u.meta.admin", '^u.meta(\\.|$)')
nil
> return string.match("u.meta.admin", '^u.meta(.|$)')
nil
See Lua regex docs:
The character % works as an escape for those magic characters.
Also, the (...|...) alternation is not supported in Lua. Instead, I guess, you need a word boundary here, like %f[set] frontier pattern:
%f[set], a frontier pattern; such item matches an empty string at any position such that the next character belongs to set and the previous character does not belong to set. The set set is interpreted as previously described. The beginning and the end of the subject are handled as if they were the character \0.
So, you can use
return string.match("u.meta.admin", '^u%.meta%f[%A]')
To only match at the end or before a .:
return string.match("u.meta", '^u%.meta%f[\0.]')
To match only if the admin is not followed with a letter or an underscore, use a negated character class [^%a_]:
return string.match("u.meta_admin", '^u%.meta%f[[^%a_]]')
See IDEONE demo to check the difference between the two expressions.
print(string.match("u.meta", '^u%.meta%f[\0.]')) -- u.meta
print(string.match("u.meta.admin", '^u%.meta%f[\0.]')) -- u.meta
print(string.match("u.meta-admin", '^u%.meta%f[\0.]')) -- nil
print(string.match("u.meta", '^u%.meta%f[%A]')) -- u.meta
print(string.match("u.meta.admin", '^u%.meta%f[%A]')) -- u.meta
print(string.match("u.meta-admin", '^u%.meta%f[%A]')) -- u.meta
-- To exclude a match if `u.admin` is followed with `_`:
print(string.match("u.meta_admin", '^u%.meta%f[[^%a_]]')) -- nil
NOTE To match the end of the string, instead of \0, you can safely use %z (as #moteus noted in his comment) (see this reference):
%z the character with representation 0

string format check

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).
# For ease of demonstration, I've moved your strings into an array
strings = [
"10$",
"10$ I am a student",
"10$Good",
"10$ Nice weekend!"
]
p strings.find_all { |s| s =~ /\A10\$[ \t]+/ }
The regular expression breaks down like this:
The / at the beginning and the end tell Ruby that everything in between is part of the regular expression
\A matches the beginning of a string
The 10 is 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 like if.
Edit: Updated white space matching as per Asmageddon's suggestion.
this works:
"10$ " =~ /^10\$ +/
and returns either nil when false or 0 when true. Thanks to Ruby's rule, you can use it directly.
Use a regular expression like this one:
/10\$\s+/
EDIT
If you use =~ for matching, note that
The =~ operator returns the character position in the string of the
start of the match
So it might return 0 to denote a match. Only a return of nil means no match.
See for example http://www.regular-expressions.info/ruby.html on a regular expression tutorial for ruby.
If you want to proceed to cases with $ and # then try this regular expression:
/^10[\$#] +/

Resources