Is it possible to do something like this th:attr="some-data=${#strings.replace(#strings.toLowerCase(object), '\\s', '-')} - if so, is there a shorter way to do this? Thanks.
That type of text manipulation is possible ... without the regex-expression (I think the problem with regex here is that thymleaf escapes the expression):
"${#strings.replace(#strings.toLowerCase(object), ' ', '-')}"
if you want to set a custom-attribute 'some-data' you can use:
th:attr="some-data=${#strings.replace(#strings.toLowerCase(object), ' ', '-')}"
or a little bit shorter for newer versions of tymeleaf:
th:some-data="${#strings.replace(#strings.toLowerCase(object), ' ', '-')}
Related
So, I have a string like this:
str1 = "blablablabla... original_url=\"https://facebook.com/125642\"> ... blablablabla..."
what is the best approach to extract this original_url?
what I have done so far is this:
original_url = str1['content'][str1['content'].index('original_url')+12..str1['content'].index('>')-2]
it works, but it seems such like a poor solution, mostly I'm stuggling to find this substring /">
here's what I have tried so far
str1.index('\">')
str1.index('\\">') # escaping only one backslach
str1.index('\\\">') # escaping both back slash and "
str1.index("\\\">") # was just without idea over here
I'm not a ruby programmer, so I'm kinda lost here
The best approach to parse xml namespaces is to use Nokogiri as suggested by #spickermann.
Quick but not elegant and not even efficient solutions:
str1 = "blablablabla... original_url=\"https://facebook.com/125642\"> ... blablablabla..."
original_url=str1[str1.index("original_url")+14...str1.index("\">")]
# => "https://facebook.com/125642"
original_url=str1.split(/original_url=\"/)[1].split(/">/).first
# => "https://facebook.com/125642"
My problem is when I use the character ', Thymeleaf converts it to '.
I need to show the apostophes instead.
My string is saved in SQL like this:
"body" : "L'' autorizzazione di EUR [[${ #numbers.formatDecimal(#strings.replace(amount,'','',''.''),1,''POINT'',2, ''COMMA'')}]] in [[${date}]] ore [[${time}]] c/o presso [[${merchant}]] รจ stata negata. [[${ #strings.replace(refuseMessage,'',/'/g)}]]"
I tried string.replace but it doesn't work. Can somebody help me please?
Are you creating HTML? Then ' is correct, and you don't need to replace it.
If you are not creating HTML, then you need to make sure your template resolver is set to an appropriate template mode, for example, TEXT:
templateResolver.setTemplateMode(TemplateMode.TEXT);
Right now I have this code:
#category.name.gsub(' ', '-').gsub('--','-').gsub('--','-')
What id does:
If I have category with name sometext sometext, it will change all space charactes to - dash characters. sometext-someteext ( I use this for url building)
.gsub('--','-').gsub('--','-') - this part I need for the case when name is something like
sometext - sometext so without this part my method will give me wrong output like sometext---sometext
So what is a more elegant way to rewrite that 3 gsubs into one?
Regex to the rescue:
.gsub(/ \-+ /, ' - ')
ActiveSupport::Inflector contains parameterize which is a more general for building urls.
> 'sometext - sometext'.parameterize
=> "sometext-sometext"
A fairly basic question :
I would like to create a string initialized to a dynamically decided number of spaces in dart.
Here's something that worked :
String spaces(n) {
var result = new List<int>.filled(n+1,32);
return new String.fromCharCodes(result);
}
Is there a better way?
Well you can always fill the list with spaces and join them:
String spaces(n) => new List.filled(n + 1, ' ').join();
This seems quite concise and easy to interpret:
''.padRight(32, ' ')
Try it in DartPad
I'm trying to create a BBcode [code] tag for my rails forum, and I have a problem with the expression:
param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>\1</pre>' )
How do I get what the regex match returns (the text inbetween the [code][/code] tags), and escape all the html and some other characters in it?
I've tried this:
param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>' + my_escape_function('\1') + '</pre>' )
but it didn't work. It just passes "\1" as a string to the function.
You should take care of the greedy behavior of the regular expressions. So the correct code looks like this:
html.gsub!(/\[(\S*?)\](.*?)\[\/\1\]/) { |m| escape_method($1, $2) }
The escape_method then looks like this:
def escape_method( type, string )
case type.downcase
when 'code'
"<pre>#{string}</pre>"
when 'bold'
"<b>#{string}</b>"
else
string
end
end
Someone here posted an answer, but they've deleted it.
I've tried their suggestion, and made it work with a small change. Whoever you are, thanks! :)
Here it is
param_string.gsub!( /\[code\](.*?)\[\/code\]/im ) {|s| '<pre>' + my_escape_function(s) + '</pre>' }
You can simply use "<pre>#{$1}</pre>" for your replacement value.