I want to print item.rate but only with having integer values in it.Currently it is printing the rate and also the currency in character.I want to hide the chars.
<td> ${item.rate} </td>
You should be able to use Freemarker's replace function. See https://freemarker.apache.org/docs/ref_builtins_string.html#ref_builtin_replace for reference.
I'd recommend using a regular expression to find everything that's not a number. That should look something like:
${item.rate}?replace('\D', '', 'r')
Reference for Freemarker's regular expression syntax is here: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Related
I have a string which consists of color codes for some of the words. For example:
[38;2;139;0;0mHello [38;2;255;255;255m[38;2;128;128;128mWorld [38;2;255;255;255m
I need a way to remove these codes. Color code values are dynamics and keep changing but it has a pattern of [38;2;r;g;bm. The regular expression, using the above string, should return 'Hello World'.
I tried regular expression ^.*$ so far but it did not work.
I would like to do this in Python, not Perl, sed or Bash.
Any suggestion how can it be done? Or a valid regex to replace with ''.
You can create a regular expression for those color codes then use the Pattern.sub() method, passing an empty string as the first argument, to remove those unwanted parts.
A regular expression which matches the pattern provided ([38;2;r;g;bm):
\[\d{,3}(;\d{,3}){4}m
Using this regular expression with the sub() method and the test string provided:
>>> import re
>>> regex = re.compile(r"\[38;2(;\d{,3}){3}m")
>>> regex.sub("", "[38;2;139;0;0mHello [38;2;255;255;255m[38;2;128;128;128mWorld [38;2;255;255;255m")
'Hello World '
I want to write a xquery statement that returns a number plus a string called " books". Like this:
<table>
<tr><td>300 books</td></tr>
<tr><td>145 books</td></tr>
</table>
I've started writing the code, but it doesn't work...
<tr><td>{$num + 'books'}</td>
Any help with this?
Move the literal string " books" outside of the expression:
<tr><td>{$num} books</td></tr>
Or you could create a sequence of items by enclosing it inside of parenthesis and separating with a comma:
<tr><td>{($num, 'books')}</td></tr>
Or you could concatenate the items (notice the leading space):
<tr><td>{concat($num, ' books')}</td></tr>
I'm trying to read a string in a specific format
RealSociedad
this is one example of string and what I want to extract is the name of the team.
I've tried something like this,
houseteam = sscanf(str, '%s');
but it does not work, why?
You can use regexprep like you did in your post above to do this for you. Even though your post says to use sscanf and from the comments in your post, you'd like to see this done using regexprep. You would have to do this using two nested regexprep calls, and you can retrieve the team name (i.e. RealSociedad) like so, given that str is in the format that you have provided:
str = 'RealSociedad';
houseteam = regexprep(regexprep(str, '^<a(.*)">', ''), '</a>$', '')
This looks very intimidating, but let's break this up. First, look at this statement:
regexprep(str, '^<a(.*)">', '')
How regexprep works is you specify the string you want to analyze, the pattern you are searching for, then what you want to replace this pattern with. The pattern we are looking for is:
^<a(.*)">
This says you are looking for patterns where the beginning of the string starts with a a<. After this, the (.*)"> is performing a greedy evaluation. This is saying that we want to find the longest sequence of characters until we reach the characters of ">. As such, what the regular expression will match is the following string:
<ahref="/teams/spain/real-sociedad-de-futbol/2028/">
We then replace this with a blank string. As such, the output of the first regexprep call will be this:
RealSociedad</a>
We want to get rid of the </a> string, and so we would make another regexprep call where we look for the </a> at the end of the string, then replace this with the blank string yet again. The pattern you are looking for is thus:
</a>$
The dollar sign ($) symbolizes that this pattern should appear at the end of the string. If we find such a pattern, we will replace it with the blank string. Therefore, what we get in the end is:
RealSociedad
Found a solution. So, %s stops when it finds a space.
str = regexprep(str, '<', ' <');
str = regexprep(str, '>', '> ');
houseteam = sscanf(str, '%*s %s %*s');
This will create a space between my desired string.
I'm writing some rows to a text file using groovy (grails 1.3.7) and I want to format the currency like this example output:
$100,000,000.00
$9,123,123.25
$10.20
$1,907.23
So basically right-justified, or left padded, with the dollar sign in front of the number so they all line up like the above. The first number is the longest we would expect to see. Right now I have an amount variable that is simply defined with a def and not string or number or anything specific like that but I can obviously change that if need be. Thanks!
You probably want to use NumberFormat.getCurrencyInstance(). This will return a NumberFormat object that uses the standard currency representation for your default Locale (or optionally, the one you pass in).
To right justify, you can use String.padLeft().
Example:
def formatter = java.text.NumberFormat.currencyInstance
def values = [0, 100000000, 9123123.25, 10.20, 1907.23]
def formatted = values.collect { formatter.format(it) }
def maxLen = formatted*.length().max()
println formatted.collect { it.padLeft(maxLen) }.join("\n")
//output
$0.00
$100,000,000.00
$9,123,123.25
$10.20
$1,907.23
In grails soemthing like this will format it nicely with comma separators.
<g:formatNumber number="${150000}" type="currency" currencyCode="USD"/>
For right aligning I would use style:
<td style='text-align:right;...'>
I Have some text file. theses texts contain a string like this(a part of text):
<abbr class="word p1"">dd</abbr>
<img src"D:\Images\1.png">
<abbr class="word p1">dd</abbr>
<img src"D:\ticket\t\1.png">
In each text file,(D:\Images\1.png) png name is different but it is always numbers(from 1 to 114)for example(1,2,3,10,...)
I want to replace this text D:\Images\[number].png with a specific text for expample:
string newtext=Replace("D:\Images\[number].png","Something");
How can i do this?
thanks.
Use a regular expression:
string newtext = Regex.Replace(text, #"(D:\\Images\\)\d+(.png)","$1Something$2");
It will replace the full match, including D:\Images\ and .png, so $1 and $2 puts back what's caught by the parentheses, so that Somthing only replaces the digits.
Use regular expressions that are represented mostly be the Regex class. See these links:
http://www.codeproject.com/Articles/93804/Using-Regular-Expressions-in-C-NET
http://msdn.microsoft.com/en-us/library/ms228595%28v=vs.80%29.aspx