I am trying to split a string in velocity context to get an array in return like following--
#if($stringValue.split("::")[1].length()==0)
//some code
But it does not work for velocity.I am getting a parser error which is unable to compile []
So,how can I implement this logic in velocity???
Using velocity 1.7 and possibly below this can be done using the String split() method.
Unlike it's Java counterpart for special characters one doesn't need to escape the forward slash (.e.g "\\|").
#set ($myString = “This|is|my|dummy|text”)
#set ($myArray = $myString.split("\|")) or
#set ($myArray = $myString.split('\|')) or
#set ($myArray = $myString.split("[|]"))
Note 1: To get the size of the array use: $myArray.size()
Note 2: To get actual values use $myArray.get(0) or $myArray[0] … etc
Suggestion: one could use beforehand #if ($myString.indexOf(‘|’)) ... #end
Related
I have an issue with extracting a substring in velocity.
the string I have is 1M/1Y (the variable string here)
I need to extract 1M and 1Y.
what is the best way to do it?
#set($index=$string.index('/'))
#set($val=$string.substring($index,index+2))
what am I doing wrong here?
In velocity template we have access to all the public methods of the String class.
Try using the below code
#set ($index = $string.indexOf('/'))
#set ($val1= $string.substring(0, $index))
#set ($index = $index + 1)
#set ($val2 = $string.substring($index))
or you can also make use of $string.split("/") if you are using Velocity 1.7
You can use stringUtil:
#set($parts = $stringUtil.split($string, "/"))
$parts.get(1)
$parts.get(2)
....
You missed $ before the last 'index' variable, this should fix your code:
#set($index=$string.index('/'))
#set($val=$string.substring($index,$index+2))
I'm writing a pig script to process an access log from a sophos proxy.
Each line is like:
2015:01:13-00:00:01 AR-BADC-FAST-01 httpproxy[27983]: id="0001" severity="info" sys="SecureWeb" sub="http" name="http access" action="pass" method="GET" srcip="10.20.7.210" dstip="10.24.2.7" user="" ad_domain="" statuscode="302" cached="0" profile="REF_DefaultHTTPProfile (Default Web Filter Profile)" filteraction="REF_DefaultHTTPCFFAction (Default content filter action)" size="0" request="0x9ac68d0" url="http://www.google.com" exceptions="av,auth,content,url,ssl,certcheck,certdate,mime,cache,fileextension" error="" authtime="0" dnstime="1" cattime="0" avscantime="0" fullreqtime="239428" device="0" auth="0"
So I managed to do it in Java with MapReduce, using the following regex: \"([^\"]*)\" to get the values between the quotes and then process it. Now I want to do the same with pig, but I'm not able to apply the regex to the each of the lines.
I'm doing:
input = load './http.log' as (line : chararray);
splt = foreach input generate FLATTEN(REGEX_EXTRACT_ALL(line,'(\\"([^\\"]*)\\")'));
dump splt;
And the result of the dump is: ().
There is something that I'm missing with the use of REGEX_EXTRACT_ALL or I have to escape some characters of the regex in a different way?
Thanks!
I managed to extract the values with a different approach, because I just wanted some of the fields of the line.
In order to get the values I'm doing:
splt = FOREACH A GENERATE
FLATTEN(REGEX_EXTRACT(line,'.*url="([^"]*)".*',1)) AS url,
FLATTEN(REGEX_EXTRACT(line,'.*fullreqtime="([^"]*)".*',1)) AS duration,
FLATTEN(REGEX_EXTRACT(line,'.*size="([^"]*)".*',1)) AS bytes;
And then I can continue with the rest of the script
I have my velocity directive set as #set ($stringList = $string.split("."))
I have tried the following in my syntax but can't get it to work
$stringList.get(0)
$stringList[0]
$stringList.[0]
${stringList}.get(0)
change
#set ($stringList = $string.split("."))
to
#set ($stringList = $string.split("\\."))
and access it like
$stringList[0]
I've inherited a java app and I'm new to it.
There's a problem where the XHTML is being escape coded, i.e. " is turning into " etc which is breaking any new Javascript I add.
Any idea how to stop the page being escape coded?
Assuming you have some HTML (before adding the scripts) & you want to decode the HTML entities, perhaps this could work:
var mycode = '<p>"A test string"</p>';
var a = document.createElement("div");
a.innerHTML = mycode;
mycode = a.innerHTML; // <p>"A test string"</p>
Maybe you can unescape it again using Apache Commons
I have this string: "\"Blah \'Blah\' Blah\"". There is another string inside it. How do I convert that into: Blah 'Blah' Blah? (you see, unescaping the string.) This is because I get a SQL Where query:
WHERE blah="Blah \'Blah\' Blah"
When I parse this, I get the string above (still inside quotes and escaped.) How would I extract that, un-escaping the string? Or is ther some much easier way to do this? Thanks,
Isaac
DO NOT DO THIS.
Follow the proper steps for parametrization of a query on your Database/Platform, and you won't have to escape anything. You also will protect yourself from injection vulnerabilities.
Put the string in a property file, Java supports XML property files and the quote character does not need to be escaped in XML.
Use loadFromXML(InputStream in) method of the Properties class.
You can then use the MessageFormat class to interpolate values into the String if needed.
This should be about right. This assumes that if it starts with a quote, it ends with a quote.
if (val.startsWith("\"") || val.startsWith("\'"))
val = val.substring(1, val.length-2);
You may wish to add val = val.trim(); as well.
"\"Blah \'Blah\' Blah\"".replaceAll("\"", "")