How to remove spaces from string in django template - django

How do you remove all spaces from a string value in a django template?
For example
<a href="www.somesite.com/lookup?id={{ order.id }}"</a>
The order number may have spaces in it and how the templates encodes them breaks the lookup function of the site. (Not our site so can't fix that end)
I'm aware that you can use order.id.strip to get rid of the spaces at the front and end but we need to remove them from the middle of the string as well.
I'm also aware that we could create a custom filter to do it but we like to avoid custom filters for one-off things like this.

From: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#cut
cut
Removes all values of arg from the given string.
For example:
{{ value|cut:" " }}
If value is "String with spaces", the output will
be "Stringwithspaces".
This worked perfectly for what I wanted. Was not easy to find so creating this question to help the google juice for the next person.
So in my example it would be:
<a href="www.somesite.com/lookup?id={{ order.id|cut:" " }}"</a>

Related

Strip dashes from a string?

For web scraping, I need to match the last part of a URL and replace "-" dashes with " " spaces.
Code looks like this...
<div class="tags">
<span class="tag" style="background-color: #5A214A;">
SA
</span>
</div>
I want to be left with "Service Assurance" (this part may contain multiple "-" dashes and require multiple replacements).
Currently being used:
Xpath:
//span[#class="tag"]/a/#href
Regex:
/.*/(.*)/
This produces "Service-Assurance", but does not strip out the "-".
I am told elsewhere that this replacement is not possible since I am already using Regex to find the string between the final "/" slashes.
Can I do both? Can I replace the "-" dashes at the end, too?
Regex is plain, inside an app called import.io, no particular language flavour.
Thank-you very much.
Try this xpath without the regex:
//*[#class='tag-wrapper']/input[1]/#value
althernatively you can also try these methods:
I scrape urls in google-sheets all the time with xpaths and regexes - so if you want to try:
=importXML("url goes here","//span[#class="tag"]/a/#href")
now then if you do at least get the url string back, then you know its working ad we can then modify it to this to get what you want:
=SUBSTITUTE(REGEXEXTRACT(importXML("url goes here","//span[#class="tag"]/a/#href"),".*\/(.*)\/$"),"-"," ")
Let me know if you have issues - there are a couple of weird quirks with google - but if you share the url your pulling that xpath in with I can at least test it myself - i use this method now more than any others, I used to use import.io and outwit hub etc a ton

How to find occurrences of a specific string within an unordered list in ColdFusion?

Our database stores a "table of contents" for each issue of our magazine as an unordered list. I want to create an "Articles related to #specificString#" page, so I'd like to query for the Table of Contents, and then find and display only those list items containing that specific string.
For example, suppose the specific string is "bumblebee," and the stored table of contents list is like so:
<ul>
<li>"The Secret Life of the Honeybee" by Anonymous</li>
<li>I got stung by a bumblebee!</li>
<li>"Flight of the Bumblee" was composed by Rimsky-Korsakov.</li>
<li>"The Case of the Disappearing Honeybee" by A. Conan Doyle, 1904</li>
</ul>
I'd like to match and display the text from the second and third list item but not the first or the fourth. I do not need to return the HTML -- only the relevant text. Conversely, if I could bleep out any list items that do NOT contain the relevant text, that would be fine as well!
I have tried
REMatchNoCase("<li>.*bumblebee.*</li>", text)
which finds all list items, even those that do not contain "bumblebee." Any suggestions or nudges in the right direction would be greatly appreciated! Many thanks!
You should use negation of the terms you want to enclosure your match instead of .*.
You can do this:
<li>[^>]*bumblebee[^<]*</li>
Here is Demo

Regex in angular- first digit not zero, but allow single zero

I want to have input element which allows one of two conditions:
Single zero can be entered
Number with max of 9 digits can be entered, but first digit shouldn't be zero
I wrote this regex (solution works in online regex testers):
/(^0$)|(^[1-9]\d{0,8}$)/
But when I use it in ng-pattern in Angular, it doesn't work.
Here is my plunker example:
http://plnkr.co/edit/iDQ7ly8ypJ3UmN5A0hJw?p=preview
Not sure if alternation is doing the problems, or I messed up the regex.
UPDATE: it seems that type="number" is causing problems. Unfortunately, I need to have this in my code, so I'm searching for solution which works with type="number".
This should work for you. I did the following:
Took out the type="number".
Gave the form a name.
Gave the input a name.
Referenced the form and input via their names instead of their id and ng-model values, respectively.
It converts the value to a number under the covers, stripping the leading zeros and converting text to 0, etc.. And the name is the correct way to access it as far as I can tell.
<form name="myForm">
<input name="myNumberField" ng-model="myNumber" ng-pattern="/(^0$)|(^[1-9]\d{0,8}$)/" required/>
<span ng-show="myForm.myNumberField.$error.pattern">Invalid pattern</span>
</form>
Here is a plunker for it.

Regex To Delete <ahref> tags but leave url

im rubbish with regex if someone could help id be very appreciative.
its going to be a bit of a tough one i imagine - so my hats off too anyone that can solve it!
so say we have file that contains 2 html tags in the following formats:
abc1234
Some Text <P>
Some Text
abc1234
im trying to remove everything in those tags except the url (and leaving other text) so the output of the regex in this document would be
abc1234
http://google.com <P>
http://www.google.com
abc1234
Can any guru figure this one out? Id prefer one regex expression to handle both cases but two seperate ones would be fine too.
Thanks in advance/
ScottStevens, it is well known that trying to parse html with regex is difficult, in fact, there is quite a verbose post on this issue. However, if those are the only two formats the <a> ever takes, here is the approach to the problem:
Your first clue on how to approach this problem is that both tags start with <a href=", and you want to take that out, and for that, a simple remove on '<a href="' will do, no regex required.
Your next clue is that sometimes, your end tag sometimes has ">...</a> and sometimes has " rel=...</a> (what goes between rel= and doesn't matter from a regex point of view). Now notice that " rel="...</a> contains within it somewhere a ">...</a>. This means you can remove " rel="...</a> in two steps, remove " rel="... up to the ">, and then remove ">...</a>. Additionally, to make sure you remove between only one tag of <a...>...</a>, add the additional constraint that in the ... of ">...</a>, there cannot be any <a.
That and a regex cheat sheet can help you get started.
That said, you should really use an html parser. Robust and Mature HTML Parser for PHP
I'm a Rubyist, so my example is going to be in Ruby. I'd recommend using two regexes, just to keep things straight:
url_reg = /<a href="(.*?)"/ # Matches first string within <a href=""> tag
tag_reg = /(<a href=.*?a>)/ # Matches entire <a href>...</a> tag
You'll want to pull the URL with the first regex out and store it temporarily, then replace the entire contents of the tag (matched with the tag_reg) with the stored URL.
You might be able to combine it, but it doesn't seem like a good idea. You're fundamentally altering (by deleting) the original tag, and replacing it with something inside itself. Less chance of things going wrong if you separate those two steps as much as possible.
Example in Ruby
def replace_tag(input)
url_reg = /<a href="(.*?)"/ # Match URLS within an <a href> tag
tag_reg = /(<a href=.*?a>)/ # Match an entire <a href></a> tag
while (input =~ tag_reg) # While the input has matching <a href> tags
url = input.scan(url_reg).flatten[0] # Retrieve the first URL match
input = input.sub(tag_reg, url) # Replace first tag contents with URL
end
return input
end
File.open("test.html", "r") do |html_input| # Open original HTML file
File.open("output.html", "w") do |html_output| # Open an output file
while line = html_input.gets # Read each line
output = replace_tag(line) # Perform necessary substitutions
html_output.puts(output) # Write output lines to file
end
end
end
Even if you don't use Ruby, I hope the example makes sense. I tested this on your given input file, and it produces the expected output.

regex for all characters on yahoo pipes

I have an apparently simple regex query for pipes - I need to truncate each item from it's (<img>) tag onwards. I thought a loop with string regex of <img[.]* replaced by blank field would have taken care of it but to no avail.
Obviously I'm missing something basic here - can someone point it out?
The item as it stands goes along something like this:
sample text title
<a rel="nofollow" target="_blank" href="http://example.com"><img border="0" src="http://example.com/image.png" alt="Yes" width="20" height="23"/></a>
<a.... (a bunch of irrelevant hyperlinks I don't need)...
Essentially I only want the title text and hyperlink that's why I'm chopping the rest off
Going one better because all I'm really doing here is making the item string more manageable by cutting it down before further manipulation - anyone know if it's possible to extract a href from a certain link in the page (in this case the 1st one) using Regex in Yahoo Pipes? I've seen the regex answer to this SO q but I'm not sure how to use it to map a url to an item attribute in a Pipes module?
You need to remove the line returns with a RegEx Pipe and replace the pattern [\r\n] with null text on the content or description field to make it a single line of text, then you can use the .* wildcard which will run to the end of the line.
http://www.yemkay.com/2008/06/30/common-problems-faced-in-yahoo-pipes/