Freemarker regular expression not working for groups? - freemarker

I have a working reg ex -> https://regex101.com/r/aX0xL6/3
that basically converts input->output
INPUT:
**1** Blah **3** Blah **I am * all bold**
DESIRED OUTPUT:
<b>1</b> Blah <b>3</b> Blah <b>I am * all bold</b>
However, I can't seem to figure out how to get this to work for freemarker?
${stringStuff?replace("\*{2}(.+?)\*{2}\", "<b>$1</b>", "r")}
see link for working example and details for basic regex match and replace
https://regex101.com/r/aX0xL6/3

This worked!
<#if lineItem?has_content>${stringStuff?replace("\\*{2}(.+?)\\*{2}", "<b>$1</b>", "r")}</#if>

Related

xpath get value attributes concat string

I have a xml with several attributes like this:
<results value="1">
<result value="111"></result>
<result value="222"></result>
<result value="333"></result>
<result value="444"></result>
</results>
Are there any way to get all the values of the attributes and concat to every value a constant string? Output like that:
Value: 111
Value: 222
Value: 333
Value: 444
Thank you very much
Using only pure xpath:
//results//result/concat("Value: ", #value)
Output:
Value: 111
Value: 222
Value: 333
Value: 444
Yes, that sounds feasible if you relies on XPath 2.0's functions library. You could write something like:
concat('Value: ',string-join((//result/#value), '\nValue: '))
or:
concat('Value: ',string-join((//result/#value), '
Value: '))
depending on how you encode the newline character.
(tested using https://www.freeformatter.com/xpath-tester.html)
Another solution − if you only have a XPath 1.0 parser at hand − would consist in just evaluating //result/#value, then post-processing the node-list result so obtained in the programming language that you use.
EDIT: if you still need to get a nodelist as a result and only rely on XPath, you should prefer #JackFleeting's answer over my first suggestion.
(BTW I had also thought about the same solution as Jack's first, and tested it on http://www.xpathtester.com/xpath but this didn't work, probably because that online parser is buggy actually).

xpath multiple nodes query with custom strings

I have a working multiple node xpath query and I want to add some custom strings between the results.
<FooBar>
<Foo>
<Fooid>A</Fooid>
<Booid>222</Booid>
<Wooid>Z</Wooid>
</Foo>
<Foo>
<Fooid>B</Fooid>
<Booid>333</Booid>
<Wooid>Y</Wooid>
</Foo>
<Foo>
<Fooid>C</Fooid>
<Booid>444</Booid>
<Wooid>X</Wooid>
</Foo>
</FooBar>
I have messed with different combinations of string-joins and/or concats, but the result was always wrong or ended up in a syntax-error. My xpath version is Xpath 2.0
//Foo/Fooid | //Foo/Booid | Foo/Wooid
The above xpath results in:
A
222
Z
My preferred result would be:
(A)
{222}
[Z]
what is the correct usage of string-join in order to get the brackets around the three ids?
after doing some research and with your comments, I was able to achive the desired solution with this line:
//Foo/concat('(', Fooid, ')'), //Foo/concat('{', Booid, '}'),Foo/concat('[', Wooid, ']')
The '|' was replaced by a comma.
to concat these characters, use their html entity instead.
concat('&lpar;', //Fooid, '&rpar;')
for parentheses use
&lpar;
&rpar;
for brackets
&lbrack;
&rbrack;
for brackes
&lbrace;
&rbrace;
See full character entity sets here

Not able to fetch data besides <Strong> tag in Robot Framework

I am trying to fetch the numeric value after strong tag, as its not an web element, I am not able to get the value 123456789 in to variable:
If I use Get Text xpath=//*[#id='referral-or-navinet-reference-number'] then the result is "Referral #: 123456789"
Please help me in getting only numeric value in to variable.
HTML Code:
<td class="normal-text" id="referral-or-navinet-reference-number" align="right">
<strong>Referral #:</strong> 123456789
</td>
You can directly use split method of python
Like :-
x.split(":") // x is a string variable of your gettext
http://www.tutorialspoint.com/python/string_split.htm
http://www.pythonforbeginners.com/dictionary/python-split
Hope it will help you :)
If your td only contains the wanted text as content text you may use the following xpath:
//*[#id='referral-or-navinet-reference-number']/text()
This should return 123456789 (perhaps with some whitespace)
You can use given xpath :
//td[#id="referral-or-navinet-reference-number"]/text()[normalize-space()]

Insert html tag between word that starts with # symbol in ruby regex

link = "http://github.com/"
message = "Hi #freedom and #cake please review my commit."
expected = "Hi <a href='http://github.com/freedom'>#freedom</a> and <a href='http://github.com/cake'>#cake</a> please review my commit."
How can I achieve the expected string above using the link and message values?
So far I can extract the username without the # symbol into Array.
links = []
message.scan(/#\b[^#][a-z]*\b/).each{|x| links << x.sub('#','')}
2.1.5 :010 > links
=> ["freedom", "cake"]
But I don't have any idea on how to insert link plus links into the message string.
Here is a sample code that you can use:
puts "Hi #freedom and #cake please review my commit.".gsub(/#([a-z]+)/i, "<a href='http:\/\/github.com\/\\1'>#\\1<\/a>")
Output:
Hi <a href='http://github.com/freedom'>#freedom</a> and <a href='http://github.com/cake'>#cake</a> please review my commit.
Tested on TutorialsPoint.
You can fine-tune the regex to /(?<=^|\s)#([a-z]+)/i to only match #something after a space or at the beginning of a string.
I don't know Ruby but usually you can work with capture-groups and reference them in the substitution text like in this regex101. So I changed the regex slightly:
#\b([^#][a-z]*)\b
Debuggex Demo
I would say your regex is needlessly complicated though, I would do this to do it simpler and also accept numbers and underscore in their username (not uncommon):
#(\w+)

I need a regex to find a url which is not inside any html tag or an attribute value of any html tag

I have html contents in following text.
"This is my text to be parsed which contains url
http://someurl.com?param1=foo&params2=bar
<a href="http://thisshouldnotbetampered.com">
some text and a url http://someotherurl.com test 1q2w
</a> <img src="http://someasseturl.com/abc.jpeg"/>
<span>i have a link too http://someurlinsidespan.com?xyz=abc </span>
"
Need a regex that will convert plain urls to hyperlink(without tampering existing hyperlink)
Expected result:
"This is my text to be parsed which contains url
<a href="http://someurl.com?param1=foo&params2=bar">
http://someurl.com?param1=foo&params2=bar</a>
<a href="http://thisshouldnotbetampered.com">
some text and a url http://someotherurl.com test
1q2w </a> <img src="http://someasseturl.com/abc.jpeg"/>
<span>i have a link too http://someurlinsidespan.com?xyz=abc </span> "
Disclaimer: You shouldn't use regex for this task, use an html parser. This is a POC to demonstrate that it's possible if you expect a good formatted HTML (which you won't have anyway).
So here's what I came up with:
(https?:\/\/(?:w{1,3}.)?[^\s]*?(?:\.[a-z]+)+)(?![^<]*?(?:<\/\w+>|\/?>))
What does this mean ?
( : group 1
https? : match http or https
\/\/ : match //
(?:w{1,3}.)? : match optionally w., ww. or www.
[^\s]*? : match anything except whitespace zero or more times ungreedy
(?:\.[a-z]+)+) : match a dot followed by [a-z] character(s), repeat this one or more times
(?! : negative lookahead
[^<]*? : match anything except < zero or more times ungreedy
(?:<\/\w+>|\/?>) : match a closing tag or /> or >
) : end of lookahead
) : end of group 1
regex101 online demo
rubular online demo
Maybe you could do a search-and-replace first to remove the HTML elements. I don't know Ruby, but the regex would be something like /<(\w+).*?>.*?</\1>/. But it might be tricky if you have nested elements of the same type.
Maybe try http://rubular.com/ .. there are some Regex tips helps you get the desired output.
I would do something like this:
require 'nokogiri'
doc = Nokogiri::HTML.fragment <<EOF
This is my text to be parsed which contains url
http://someurl.com <a href="http://thisshouldnotbetampered.com">
some text and a url http://someotherurl.com test 1q2w </a> <img src="http://someasseturl.com/abc.jpeg"/>
EOF
doc.search('*').each{|n| n.replace "\n"}
URI.extract doc.text
#=> ["http://someurl.com"]

Resources