startswith ends with and containing in Apache free marker - freemarker

How to check whether particular string starts with hello or ends with Hello or contains hello in
apache free marker. is there any predefined function available
HelloIamShruti
IamShrutiHello
ShrutiHelloSharma
If i want to check starts with "Hello" first string should be returned
If i want to check ends with "Hello" second string should be returned
If i want to check contains "Hello" last string should be returned

Freemarker methods available for your requests are:
starts_with, ex. "redblue"?starts_with("red") will return true.
ends_with, ex. "redblue"?ends_with("red") will return false.
contains, ex. <#if "piceous"?contains("ice")>It contains "ice"</#if> will show the message
When you want the word to be contained but not in the start or the end, just add an if condition to check if starts_with and ends_with result to false.

Related

Extracting a substring from a string using `Regexp.new`

I have a string like this:
var = "Renewal Quote RQ00041233 (Payment Pending) Policy R38A014294-1"
I have to extract "Payment Pending" from that string using only the information included in another single string.
The following:
var[/\((.*)\)/, 1]
will extract what I want. I can include the string representation of the regex in the string to be given, and construct the regular expression from it using Regexp.new, but I have no way to achieve the information 1 used as the second argument of [].
Without the second argument 1,
regex_string = '\((.*)\)'
var[Regexp.new(regex_string)]
fetches the string "(Payment Pending)"instead of the expected "Payment Pending".
Can someone help me?
Not sure what you are trying to do, but you can get rid of capturing groups using a different regex:
var[/(?<=\().*(?=\))/]
# => "Payment Pending"
or
var[Regexp.new('(?<=\().*(?=\))')]
# => "Payment Pending"
/\((.*)\)/ is just shorthand for Regexp.new('\((.*)\)').
String#[] takes a regex and a capture group as two separate arguments. var[/\((.*)\)/, 1] is var[Regex, 1].
The important thing to realize is 1 is passed to var[], not the regex.
re = Regexp.new('\((.*)\)')
match = var[re, 1]
Note: you might want to require a named capture group rather than a numbered one. It's very easy to accidentally include an extra capture group in a regex.
Assuming there are no nested parenthesis in the string, one way to do that without using a regular expression is as follows.
instance_eval "var[(i=var.index('(')+1)..var.index(')',i)-1]"
#=> "Payment Pending"
See String#index, particularly the reference to the optional second argument, "offset".

String matching regex syntax issue

I am trying to write a method to pulls out every string that matches the criteria from a large text file:
Every element is separated by a comma
The first 5 elements could be any number from 1-59
The next 21 elements should be numbers from 1-5
The next 27 elements could be either true or false (no caps)
The final 5 elements are integers from 1-5
My code:
#this string should be returned by the regex matching
str="3,15,14,31,40,5,5,4,5,3,4,4,5,2,2,2,1,2,1,1,3,3,3,2,4,3,false,false,false,false,false,true,false,true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,3,3,3,2,3"
matchResult=/[1-59]{5}[1-5]{21}[true|false]{27}[1-5]{5}/.match(str)
matchResult.each{|x| #this doesnt work....why?
puts x
}
What is the correct way to print all matches? matchResult.each throws an error. I thought it returned an array of matches.
How can I adjust my regex to expect a comma between every value (not at the ends of the string)?
Put true or false inside a capturing group or non-capturing group like (?:true|false) so that it would match the exact true or false substring , this [true|false] will match a single character only whether it may be t or r or u or e or | ,....
> str="3,15,14,31,40,5,5,4,5,3,4,4,5,2,2,2,1,2,1,1,3,3,3,2,4,3,false,false,false,false,false,true,false,true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,3,3,3,2,3"
> str.match(/^(?:[1-5]\d|[1-9])(?:,(?:[1-5]\d|[1-9])){4}(?:,[1-5]){21}(?:,(?:true|false)){27}(?:,[1-5]){5}$/)
=> #<MatchData "3,15,14,31,40,5,5,4,5,3,4,4,5,2,2,2,1,2,1,1,3,3,3,2,4,3,false,false,false,false,false,true,false,true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,3,3,3,2,3">
In regards to your first question:
"What is the correct way to print all matches? matchResult.each throws an error. I thought it returned an array of matches."
The Regex .match method doesn't return an array of matches; it returns either a match object (in this case, a string, since you're calling .match on a string) or nil if there are no matches (see docs here).
This means matchResult is a string, and you can't call .each on a string, which is why you're getting an error message. See this post for more info on .each & strings.

Validating User Input for Letters

I've recently been trying to validate a user input so that only letters from the alphabet are accepted, how would I do this? I know how to validate user input for most things, but this one line of code for letters is really troubling me.
You can check the contents of a field with this function:
function validate theString
return matchText(theString,"^[a-zA-Z]+$")
end validate
^[a-zA-Z]+$ is a regular expression. ^indicates the beginning of a string, the brackets equal one char and the expression inside the bracket determine a set of characters. The + means that all following characters have to be equal to the preceding (set of) character(s). The $ indicates the end of the string. In other words, according to this expression, all characters must be of the set a up to and including z or A up to and including Z.
matchText() is a LiveCode function, which checks if the string in the first parameter matches the regular expression in the second parameter. Put the validate() function somewhere at card or stack level and call it from a field in a rawKeyUp handler:
on rawKeyUp
if not validate(the text of me) then
beep
answer "Sorry, that's wrong"
end if
end rawKeyUp
You could also check beforehand:
on keyDown theKey
if validate(theKey) then
pass keyDown
end if
end keyDown
This method is slightly verbose. You could also put the matchText function in a keyDown handler of your field.

How do I search an array using a partial string, and return the index?

I want to search through an array using a partial string, and then get the index where that string is found. For example:
a = ["This is line 1", "We have line 2 here", "and finally line 3", "potato"]
a.index("potato") # this returns 3
a.index("We have") # this returns nil
Using a.grep will return the full string, and using a.any? will return a correct true/false statement, but neither returns the index where the match was found, or at least I can't figure out how to do it.
I'm working on a piece of code that reads a file, looks for a specific header, and then returns the index of that header so it can use it as an offset for future searches. Without starting my search from a specific index, my other searches will get false positives.
Use a block.
a.index{|s| s.include?("We have")}
or
a.index{|s| s =~ /We have/}

Why doesn't this Ruby replace regex work as expected?

Consider the following string which is a C fragment in a file:
strcat(errbuf,errbuftemp);
I want to replace errbuf (but not errbuftemp) with the prefix G-> plus errbuf. To do that successfully, I check the character after and the character before errbuf to see if it's in a list of approved characters and then I perform the replace.
I created the following Ruby file:
line = " strcat(errbuf,errbuftemp);"
item = "errbuf"
puts line.gsub(/([ \t\n\r(),\[\]]{1})#{item}([ \t\n\r(),\[\]]{1})/, "#{$1}G\->#{item}#{$2}")
Expected result:
strcat(G->errbuf,errbuftemp);
Actual result
strcatG->errbuferrbuftemp);
Basically, the matched characters before and after errbuf are not reinserted back with the replace expression.
Anyone can point out what I'm doing wrong?
Because you must use syntax gsub(/.../){"...#{$1}...#{$2}..."} or gsub(/.../,'...\1...\2...').
Here was the same problem: werid, same expression yield different value when excuting two times in irb
The problem is that the variable $1 is interpolated into the argument string before gsub is run, meaning that the previous value of $1 is what the symbol gets replaced with. You can replace the second argument with '\1 ?' to get the intended effect. (Chuck)
I think part of the problem is the use of gsub() instead of sub().
Here's two alternates:
str = 'strcat(errbuf,errbuftemp);'
str.sub(/\w+,/) { |s| 'G->' + s } # => "strcat(G->errbuf,errbuftemp);"
str.sub(/\((\w+)\b/, '(G->\1') # => "strcat(G->errbuf,errbuftemp);"

Resources