Replace new line character in freemarker - html

Is there any way to replace the new line character on free marker? I am trying this:
<#assign str = str?replace("(\r\n)+", "</p><p>")>
which worked in Java, but not in freemarker. How can I do this?

Ok, I found the problem. The replace function needs to know if the expresion is a regex, so I had to add 'r' as a parameter
<#assign str = str?replace("(\r\n)+", "</p><p>",'r')>

Related

Why is the source location end off by two characters here?

I'm trying to write a source to source translator using libTooling.
I'm using ASTMatchers to try to find if statements that don't have curly braces and then use a rewriter to add the braces.
The matcher I'm using is:
ifStmt(unless(hasDescendant(compoundStmt())))
Then I just get the start and end locations, and rewrite the curly braces.
Here's the source code for that:
if (const IfStmt *IfS = Result.Nodes.getNodeAs<clang::IfStmt>("ifStmt")) {
const Stmt *Then = IfS->getThen();
Rewrite.InsertText(Then->getLocStart(), "{", true, true);
Rewrite.InsertText(Then->getLocEnd(),"}",true,true);
Now the problem is that for some reason the end location is always off by 2 characters. Why is this so?
the SourceLocation i was getting is off by one because it only matches the token and ";" is not part of that.
btw, if anybody's wondering how to include the ";" into the range if they want to, you could just use Lexer::MeasureTokenLength and then add that by one and get the new SourceLocaiton by offset.

Regex doesn't find a match with a pattern

In a C# project, Regex is behaving weirdly for me.
I have this method:
string RegTest()
{
string HTML = "<input type=\"hidden\" name=\"authenticity_token\" value=\"d27956cca6b75db4d8dd502d0569dd246455131c\">";
Regex AuthRegex = new Regex(#"name=""authenticity_token"" value=""([A-Ba-b0-9/-]+)""");
string Auth = AuthRegex.Match(HTML).Value;
return Auth;
}
For a reason I don't understand at all, the Regex doesn't find any match with this pattern. It just returns "".
How can I fix this?
The problem is:
[A-Ba-b0-9/-]+
What character ranges (x-y) basically do is get a set of all characters in between. In other words, a-b = all letters between a and b, aka only a and b. However,
d27956cca6b75db4d8dd502d0569dd246455131c
looks like a hex. Therefore, you should use
[A-Fa-f0-9-]+
instead.

How to get rid of new line character in a string, ActionScript3

I want to remove the new line character in a string, I tried to use something like:
myString.replace('\n', '');
But this doesn't work. What's the best way to do it?
A new string is returned, it is not modified in-place.
myString = myString.replace('\n', '');
If you have multiple newlines, you'll need to use a RegExp with the g flag set.
myString = myString.replace(new RegExp('\\n', 'g'), '');
And if you don't want to use RegExp you can simply use split and join:
myString = myString.split('\n').join('');

String.replace() function to parse XML string so that it can be displayed in HTML

I have a XML string which needs to be displayed within HTML. I understand the first thing needed to be done here is to convert all '<' and '>' into '& lt;' and '& gt;' (ignore the space after & sign). This is what I am doing to replace '<' -
regExp = new RegExp("/</g");
xmlString = xmlString.replace(regExp, '& lt;');
xmlString does not change.
Also, trace(regExp.test("<")); prints false.
What is wrong here?
replace returns a new string, it doesn't modify the old one. So if you want to overwrite the old you have to do the following:
xmlString = xmlString.replace(regExp, '<');
Or if you don't want to overwrite the old one, just store the result in a new variable.
var newString = xmlString.replace(regExp, '<');
The issue is the way you create your RegExp object.
Because your using the RegExp constructor, don't include the / characters:
regExp = new RegExp("<", "g");
or use / as a shortcut:
regExp = /</g;
See this page for more details: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/RegExp.html

Zend Framework dom problem

I want to get website shortcut icon(favicon) and stylesheet path with zend_dom query
$dom = new Zend_Dom_Query($html);
$stylesheet = $dom->query('link[rel="stylesheet"]');
$shortcut = $dom->query('link[rel="shortcut icon"]');
Stylesheet query is work but shortcut icon query not work. How i do?
Thanks.
This appears to be an issue with Zend's css style query implementation. In Zend/Dom/Query.php, the query function calls a conversion function to convert the query into proper xpath format:
public function query($query)
{
$xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
return $this->queryXpath($xpathQuery, $query);
}
However within the transform() method, they seem to be using some pretty basic regex to split up the string by spaces:
$segments = preg_split('/\s+/', $path);
Which basically means your link[rel="shortcut icon"] query now becomes two queries: link[rel="shortcut and icon"]
To get around this, you can use the method Zend_Dom_Query::queryXpath() and provide it with a proper xPath query. Like this:
$dom->queryXpath('//link[#rel="shortcut icon"]');