How to replace a variable number of instances in a string? - postgresql

A front end WYSIWYG editor in our application has been producing strings with gobs of <p> tags in the editable string. Every time the records were updated, the <p> tags would double. How can I fix the data, reducing n number of consecutive <p> tags in the string with a single one?
Change this:
<p><p>Hello, world
or this:
<p><p><p><p><p><p><p><p><p>Hello, world
to this:
<p>Hello, world

You can do it with using regular expression.
Example:
select regexp_replace('<p><p><p><p><p><p><p><p><p>Hello, world', '(<p>)\1{1,}', '\1', 'g');
Result:
<p>Hello, world

Related

Remove html tags in subreport

I have a subreport in SSRS which returns a text embedded in HTML tags. I will like to know if there is a way of stripping these HTML tags so as to only have the text. I am using VS 2008.
I have tried using a regex function as below to strip the HTML tags but this does not work:
Shared FUNCTION RemoveHtml(ByVal Text As String) AS String
IF Text IsNot Nothing Then
Dim mRemoveTagRegex AS NEW System.Text.RegularExpressions.Regex(“<(.|\n)+?>”)
Return mRemoveTagRegex.Replace(text, "")
End If
end function
You could probably just use a combination of the built-in functions provided with SSRS to do what you need. I'd recommend combining Mid with InStr. The following expression will take the value between the last character of the opening HTML tag and the first character of the closing HTML tag.
=MID(Fields!Field.Value,
InStr(Fields!Field.Value, ">") + 1,
InStrRev(Fields!Field.Value, "</")
-Len(Left(Fields!Field.Value,
InStr(Fields!Field.Value, ">") + 1)))
Edit: It got a little more complex than I thought, but this should do the trick.

Conditional Formatting Char vs Numeric in String

I'm trying to do some simple formatting in crystal reports like:
String:
John Smith 212-212-2121
Where the A-Z would be one color and 0-9 would be a different color
I tried this
if NumericText ({myString})
then crRed else crNavy
Any ideas on how to do this ?
As far as I'm aware, you can't have multiple colors in a single string.
You've got a couple of ways of doing this, but they're both kind of clunky, and your string as-is will need to either be parsed or reformatted.
Split your string into two separate formula fields. Color the name field the appropriate color, and the phone number field with the appropriate color, and stick both of these fields into another textbox field. It should look something like this crudely drawn example:
[ [Name formula field] [Phone formula field] ]
Change your string to use HTML, and then format your textbox to use a Text Interpretation of HTML. I can't say this will work 100%, though, because Crystal Reports only supports some HTML.
In any case, your new string would look something like:
< font color='red'>John Smith< /font> < font color='blue'>212-212-2121< /font>
Assuming that they are two fields, embed both in a Text Object. You'll be able to change the text formatting of each field to be the desired color.
Another approach: place both fields on the canvas; color as desired. Add a Text Object to the canvas. Drag each field to the Text Object. This should retain the formatting, but I haven't tested it.
It you need to split the text into name and number create two formula fields. Once created, follow the steps listed earlier.

Scala/Lift - map function

I'm using Lift to generate my web front.
In the scala file I have a list: val testList = List("part1","part2","part3")
I'm apply a function to each element for the list. For now I just want to make them bold. I know there is another way to make them bold by changing the html code, but that's not the point of this exercise. I'm trying to see if I can generate the html in the scala file as opposed to the .html file.
I defined a function
def formatText(s:String)={
<B> s </B>
}
and I call var testList2= testList.map(formatText(_))
The problem is that in the output all I see is s s s in bold. If I put quotes around the <B> then the string is escapsed so instead of getting part1 (in bold), I get < B >part1< / B >.
How do I display those strings in bold? Is there a $s to tell Lift/scala I mean the variable s and no char s in formatText?
The XML-literal "escape" characters (for adding variables, expressions, etc) are { and }:
def formatText(s:String)= <B> {s} </B>
Take a look at Programming in Scala 26.3: XML Literals for more details.

retrieving NSString from array in iPhone

I have an array which contains description of a route on map. I got this array by parsing JSON. My arrays contains string in this format:
"<b>Sri Krishna Nagar Rd</b> \U306b\U5411\U304b\U3063\U3066<b>\U5317\U6771</b>\U306b\U9032\U3080",
"\U53f3\U6298\U3057\U3066\U305d\U306e\U307e\U307e <b>Sri Krishna Nagar Rd</b> \U3092\U9032\U3080",
"\U5927\U304d\U304f\U5de6\U65b9\U5411\U306b\U66f2\U304c\U308a\U305d\U306e\U307e\U307e <b>Bailey Rd/<wbr/>NH 30</b> \U3092\U9032\U3080<div class=\"\">\U305d\U306e\U307e\U307e NH 30 \U3092\U9032\U3080</div><div class=\"google_note\">\n<b landmarkid=\"0x39ed57bfe47253b7:0x779c8bf48892f269\" class=\"dir-landmark\">Petrol Bunk</b>\U3092\U901a\U904e\U3059\U308b<div class=\"dirseg-sub\">\Uff083.9 km \U5148\U3001\U53f3\U624b\Uff09</div>\n</div>",
Now I want to get name of places from this array like Sri Krishna Nagar Rd , NH 30 Petrol Bunk. First two should give Sri Krishna Nagar Rd and last on should give NH 30 Petrol
Bunk
How can I get result like this.Any help would be appreciated. Thanx In Advance.
Again, suppose I have string in this format..."\U5de6\U6298\U3059\U308b" which don't have ny place name.How will i handle this scenarios.
You can get like below:
NSString *strName=[yourArray objectAtIndex:index];
NSString *yourPlaceString=[[strName componentsSeparatedByString:#"<b>"] objectAtIndex:1];
yourPlaceString=[[yourPlaceString componentsSeparatedByString:#"</b>"] objectAtIndex:0];
you can get all places like this.
First of all, you should check if you don't have any other cleaner API available for the service you query this data. If the service returns such garbage in its JSON response, that shouldn't be your responsability to clean up that mess: the service should return some text that is more usable if it is a real clean API.
Next, if you really don't have any other choice and really need to clean this text, you have two options:
If the text is XHTML (I mean real XHTML, conforming to the XML standard) you may use an NSXMLParser to filter out any tags and only keep the text from your string. This may be a bit too much for this anyway so I don't really recommand it.
You can use regular expressions. If you are developping for iOS4.0+ you can use the NSRegularExpressionclass for this purpose. The tricky part is to get the right regex (can help you with that if needed)
You can use the NSScanner class (which is available in iOS since 2.0 IIRC) to scan characters in you string and parse it. This is probably easier to understand and the way to go if you are not a regex expert, so I recommand this approach
For example if you choose the NSScannersolution, you can scan your string for characters in the alphanumeric character set, to scan letters and digits and accumulate it (you may also add ponctuation characters to your NSCharacterSetyou are using if needed). You will have the NSScanner to stop when it encounter characters such as the unicode characters \Uxxxx or like < and >. When you encounter < you can then ask the NSScanner to ignore the characters up to the next >, then start to scan the alphanumeric characters again and accumulating... and so on until the end of the string.
Finally, if you really find a pattern in the response string you are receiving, like if your place names is always between the first <b> and </b> pair (but you have to be sure of that), you can handle it other ways, like:
splitting your string using the <b> text as the separator (e.g. componentsSeparatedByString)
or asking the rangeOfString for the string <b> and then for string </b> and once you have their position, only extract substringWithRange from your original string to extract only the place name (using rangeOfString will be faster that componentsSeparatedByString because it will stop on the first occurrence found)
It looks like an encoding problem - can you change the encoding of the source or target to a different format. I had similar issues with German ö ä ü characters when UTF-8 was turned off....

Only display one paragraph of text

You can set what the Facebook Share preview says. I would like it to be the first paragraph of my movable type entry. The people who make entries sometimes use
<p>
tags or they use the rich editor which puts in two
<br /><br />
tags to separate paragraphs.
Is there a way I can have movable type detect when the first paragraph end and only display the first paragraph? I would like to add that to my entry template so it will add some information to my head.
EntryBody has a lot of attributes to help format the output of the tag. You can use those to change the content so it shows up correctly in HTML, JavaScript, PHP, XML or other forms of output.
If you understand how to use regular expressions, you can use that and an additional language, say PHP, to break the body up into an array and only output the first paragraph or element of the array.
The simplest thing, though, I would think, would be to do something like
<mt:EntryBody words=100>
That will cut off the entry body after the first 100 words. You could also require users to upload an excerpt with the entry and use the entry excerpt for Facebook, instead.