Wrap unknown count of string using replace - c#

Sorry for the title, I had a hard time trying to summarize this.
I'd like to replace an unknown number of instances of a string with a wrapper. So I'd like to replace this:
Test with two:\t\t tab characters
With this:
Test with two:<span class="" style="white-space:pre">\t\t</span> tab characters
There could be any number of \t characters in the given string, and in multiple locations.
The reason I'm trying to do this is our software uses Chilkat to sent emails, and if HTML content contains tabs, these are not shown at the receiving end. When sent, we use \t to represent a tab, and when viewing the source of the received email, the tabs are there, but outside the source they are not:
Outside Source:
Source:
I tested with GMail, and this wraps the tabs:
I understand this maybe be a Chilkat issue, but I can't find much help on the topic, but if I can get around it as above, I'm willing to try it.

Use this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "Test with two:\t\t tab characters";
string output = Regex.Replace(input, #"(\t+)", "<span class=\"\" style=\"white-space:pre\">$1</span>");
}
}
}

This is what I ended up using. The capture group was picking up each repetition individually, and then the replace was only replacing the first found instance, not the entire capture group
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "Test with two:\t\t tab characters";
string output = Regex.Replace(input, #"(\t)+", "<span class=\"\" style=\"white-space:pre\">$&</span>");
}
}
}

Related

Capitalizing strings in C# containing html escape characters

I'm trying to title case some text that may contain html escape characters. Is there any way of doing this other than with regular expressions? Here's some example code:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string input = "B&G fried pie";
string output = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input.ToLowerInvariant());
Console.WriteLine(output); //Should be B&G Fried Pie
Console.ReadKey();
}
}
}
Another way I can think of is to replace & with &, do my title case, then replace the & with &amp.
You can use the System.Web.HttpUtility class to decode and encode html strings, so your code would then look something like:
private static string ToTitleCase(string input)
{
return input == null
? null
: HttpUtility.HtmlEncode(CultureInfo.InvariantCulture.TextInfo
.ToTitleCase(HttpUtility.HtmlDecode(input.ToLowerInvariant())));
}
And in use it would look something like:
Console.WriteLine(ToTitleCase("B&G fried pie"));

How to get HTMLElement ID if it includes a "-"

I am trying to load Google and get the ID of the searchbox. The ID of the box is "lst-ib". Which when the program goes to debug it is expecting a semicolon.
Is there a way around it to get the element id? So far I have:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
public void Main(string[] args)
{
Process.Start("www.google.com");
HtmlElement lst-ib = WebBrowser1.Document.All["foo"];
//expects a semi colon on the line above after the element id
if (lst-ib != null)
{
lst-ib.InnerText = "test";
}
Console.ReadKey();
}
}
}
That is C# code and - is not valid in identifiers. Feel free to name the variable as you wish – it has no bearing on what the ID of the element is.
The - is an operator, you cannot use this way!
Here you will find more information about operators:
https://msdn.microsoft.com/en-us/library/6a71f45d.aspx
I recomend you rename - (trace) to _ (underline) or anyway you want
=D

C# connecting (referencing) couple *.cs files

I have couple questions about referencing methods / variables between two or more *.cs files. I know that there are similar topics, but I still don't quite understand what is going on.
I'm using Visual Studio Community 2015.
So here is the problem. I have 2 files, those files are First.cs and Second.cs. They are saved in completely different, known locations on hard disc.
Inside First.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forum
{
class First
{
static void Main(string[] args)
{
}
public int GiveMeNumber()
{
return 5;
}
}
}
Inside Second.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forum
{
class Second
{
int number = // method from file First.cs
}
}
How do I access method GiveMeNumber() from First.cs in Second.cs as assignment for int number? How do I tell my compiler where are those files?
Thanks for any help :)
Like Alex said in his comment.
You can create a solution/project, add existing item, and browse to your first.cs and second.cs.
Mark both files with the public-keyword
for example:
namespace Forum
{
public class Second
{
int number = // method from file First.cs
}
}
Then both class can be used within each other.
So you could do
var first = new First();
var number = first.GiveMeNumber();
you probably want to do it the other way around, because I think you have a console app where your First class has a main-method.
does that help>?

Windows 7 clipboard copy paste operation using C#

I'm working on a Windows application where I need to use clipboard data. I am trying to copy text from clipboard by the code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace MultiValuedClipBoard
{
class Class1
{
public String SwapClipboardHtmlText(String replacementHtmlText)
{
String returnHtmlText = "hello";
if (Clipboard.ContainsText(TextDataFormat.Html))
{
returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
}
return returnHtmlText;
}
}
}
Calling the above function by:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace MultiValuedClipBoard
{
class Program
{
static void Main(string[] args)
{
Class1 aas = new Class1();
string a = aas.SwapClipboardHtmlText("chetan");
Console.WriteLine(a);
Console.ReadLine();
}
}
}
When running this code it gives the output "Hello" which is the default value, not clipboard data.
Your code will not work because of two reasons:
[1] When you say:
if (Clipboard.ContainsText(TextDataFormat.Html))
Here you are basically assuming that the clipboard already contains a text and that too in HTML format, but depending on the values you are setting in the clipboard it doesn't look like you are intending to use the pre-existing clipboard value anywhere in your program. So, this if condition should not be there.
[2] Secondly, you are further trying to set the string "chetan" to the clipboard which is definitely not in HTML format. So,
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
becomes
Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
Hence, effectively, your new code becomes something like this:
String returnHtmlText = "hello";
//if (Clipboard.ContainsText(TextDataFormat.Html))
//{
returnHtmlText = Clipboard.GetText(TextDataFormat.Text);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
//}
return returnHtmlText;
Clearly Clipboard.ContainsText(TextDataFormat.Html) evaluates to false. Which means that the clipboard in fact does not contain text in the format you specify.
I changed your program to prove the point:
[STAThread]
static void Main(string[] args)
{
Clipboard.SetText("boo yah!", TextDataFormat.Html);
Class1 aas = new Class1();
string a = aas.SwapClipboardHtmlText("chetan");
Console.WriteLine(a);
Console.WriteLine(Clipboard.GetText(TextDataFormat.Html));
Console.ReadLine();
}
Output:
boo yah!
chetan

Two regex groups in one filter string

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace RegExCs
{
class Program
{
static void Main(string[] args)
{
string rawData;
Regex filter = new Regex(#"(?<ip>([0-9]+\.){3}[0-9])"+#"(?<time>(\s[0-2][0-9]:[0-9][0-9]))");
rawData=File.ReadAllText("Query list");
MatchCollection theMatches = filter.Matches(rawData);
foreach (Match theMatch in theMatches)
{
Console.WriteLine("ip: {0}\n",theMatch.Groups["ip"]);
Console.WriteLine("time: {0}\n", theMatch.Groups["time"]);
}
Console.ReadKey();
}
}
}
"Query list" file:
Reply from 212.77.100.101 www.wp.pl time: 21:37
Reply from 111.41.130.55 www.cnn.com time: 05:33
Reply from 230.77.100.101 www.piting.com time: 04:12
Reply from 65.77.100.101 www.ha.org time: 12:55
Reply from 200.77.100.101 www.example.com time: 07:56
This program compiles and runs, but the empty console window is opened the whole time. Why?
Because nothing matches to the regexp
#"(?<ip>([0-9]+\.){3}[0-9])(?<time>(\s[0-2][0-9]:[0-9][0-9]))"
You just concatenate 2 strings, and the compound regex expects the string to be ip followed by time without anything else (even a space) between them.
You need to change it to
#"(?<ip>([0-9]+\.){3}[0-9]).*(?<time>(\s[0-2][0-9]:[0-9][0-9]))"
^------- "anything" between first and second group

Categories