Replace part of text with html text not working - c#

I want to replace matching keyword in text with same keyword but wrapped with <span></span>
example : This is the sample text to be searched
replaced text should be line
This is the <span class="match">sample</span>text to be searched
I am using following code but its not working
protected String getTitle(object title)
{
string sTitle = title.ToString();
Regex regex = null;
string pattern = #"(\b(?:" + _Keyword.ToString().Trim() + #")\b)(?![^<]*?>)";
regex = new Regex(pattern);
sTitle = regex.Replace(sTitle, "<span class='keyword-highlight'>" + _Keyword + "</span>");
return sTitle;
}
above code replace whole text with keyword not just the matching part

One reason could be, you are getting some wrong value in 'sTitle'. Example: sTitle = "sample"

If the problem is the case of sample, you can use:
regex = new Regex(pattern, RegexOptions.IgnoreCase);

Replace this:
regex = new Regex(pattern);
With this:
regex = new Regex(pattern,RegexOptions.IgnoreCase);
I run this code:
string _Keyword = "Sample";
string sTitle = " This is the sample text to be searched";
Regex regex = null;
string pattern = #"(\b(?:" + _Keyword.ToString().Trim() + #")\b)(?![^<]*?>)";
regex = new Regex(pattern,RegexOptions.IgnoreCase);
sTitle = regex.Replace(sTitle, "<span class='keyword-highlight'>" + _Keyword + "</span>");
and result is:
This is the <span class='keyword-highlight'>sample</span> text to be searched

Related

regex to exclude if preceded by string?

I haven't used regex much before but found something useful on the net that I'm using:
private string ConvertUrlsToLinks(string msg)
{
string regex = #"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])";
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
return r.Replace(msg, "$1").Replace("href=\"www", "href=\"http://www").Replace(#"\r\n", "<br />").Replace(#"\n", "<br />").Replace(#"\r", "<br />");
}
It does a good job but now I want it to exclude urls that already have a "a href=" in front. There's the ending "/a" to consider too.
Can that be done with regex or have to use totally different approach, like coding?
Try this:
((?<!href=')(?<!href=")(www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])
I tested on regex101.com
With the following sample set:
www.google.com
http://hi.com
http://www.fishy.com
href='www.ignore.com'
www.ouch.com
Using your existing regex pattern you could make a few simple changes to handle additional text being prepended or appended to your string:
`.+` <- pattern -> `(.+)?`
Which would give you:
.+((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])(.+)?
So passing the string of either:
<a href='http://www.test.com'>http://www.test.com</a>
...or...
http://www.test.com
Would result in:
www.test.com
Examples:
https://regex101.com/r/bO0cW6/1
http://ideone.com/suVw3I
I think it would be a little ToNy tHe pOny to do that in regex after all, so wrote the code, in case anyone is interested here it is:
private string handleatag(string msg, string tagbegin, string tagend)
{
ArrayList tags = new ArrayList();
int tagbeginpos = msg.IndexOf(tagbegin);
int tagendpos;
string hash = tagbegin.GetHashCode().ToString();
while (tagbeginpos != -1)
{
tagendpos = msg.IndexOf(tagend, tagbeginpos);
if (tagendpos != -1)
{
string atag = msg.Substring(tagbeginpos, tagendpos - tagbeginpos + tagend.Length);
msg = msg.Replace(atag, hash + tags.Count.ToString());
tags.Add(atag);
}
else
msg = msg.Remove(tagbeginpos, tagbegin.Length);
tagbeginpos = msg.IndexOf(tagbegin, tagbeginpos);
}
msg = ConvertUrlsToLinks(msg);
for (int i = 0; i < tags.Count; i++)
msg = msg.Replace(hash + i.ToString(), tags[i].ToString());
return msg;
}
private string ConvertUrlsToLinks(string msg)
{
if (msg.IndexOf("<a href=") != -1)
return handleatag(msg, "<a href=", "</a>");
string regex = #"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])";
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
return r.Replace(msg, "$1").Replace("href=\"www", "href=\"http://www").Replace(#"\r\n", "<br />").Replace(#"\n", "<br />").Replace(#"\r", "<br />");
}

Replacement in a String with a regular expression

I'm trying to replace a string in C# with the class Regex but I don't know use the class properly.
I want replace the next appearance chain in the String "a"
":(one space)(one or more characters)(one space)"
by the next regular expression
":(two spaces)(one or more characters)(three spaces)"
Will anyone help me and give me the code and explains me the regular expresion used?
you can use string.Replace(string, string)
try this one.
http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
try this one
private String StrReplace(String Str)
{
String Output = string.Empty;
String re1 = "(:)( )((?:[a-z][a-z]+))( )";
Regex r = new Regex(re1, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(Str);
if (m.Success)
{
String c1 = m.Groups[1].ToString();
String ws1 = m.Groups[2].ToString() + " ";
String word1 = m.Groups[3].ToString();
String ws2 = m.Groups[4].ToString() + " ";
Output = c1.ToString() + ws1.ToString() + word1.ToString() + ws2.ToString() + "\n";
Output = Regex.Replace(Str, re1, Output);
}
return Output;
}
Using String.Replace
var str = "Test string with : .*. to replace";
var newstr = str.Replace(": .*. ", ": .*. ");
Using Regex.Replace
var newstr = Regex.Replace(str,": .*. ", ": .*. ");

Regex Replace on a JSON structure

I am currently trying to do a Regex Replace on a JSON string that looks like:
String input = "{\"`####`Answer_Options11\": \"monkey22\",\"`####`Answer_Options\": \"monkey\",\"Answer_Options2\": \"not a monkey\"}";
a
The goal is to find and replace all the value fields who's key field starts with `####`
I currently have this:
static Regex _FieldRegex = new Regex(#"`####`\w+" + ".:.\"(.*)\",");
static public string MatchKey(string input)
{
MatchCollection match = _encryptedFieldRegex.Matches(input.ToLower());
string match2 = "";
foreach (Match k in match )
{
foreach (Capture cap in k.Captures)
{
Console.WriteLine("" + cap.Value);
match2 = Regex.Replace(input.ToLower(), cap.Value.ToString(), #"CAKE");
}
}
return match2.ToString();
}
Now this isn't working. Naturally I guess since it picks up the entire `####`Answer_Options11\": \"monkey22\",\"`####`Answer_Options\": \"monkey\", as a match and replaces it. I want to just replace the match.Group[1] like you would for a single match on the string.
At the end of the day the JSON string needs to look something like this:
String input = "{\"`####`Answer_Options11\": \"CATS AND CAKE\",\"`####`Answer_Options\": \"CAKE WAS A LIE\",\"Answer_Options2\": \"not a monkey\"}";
Any idea how to do this?
you want a positive lookahead and a positive lookbehind :
(?<=####.+?:).*?(?=,)
the lookaheads and lookbehinds will verify that it matches those patterns, but not include them in the match. This site explains the concept pretty well.
Generated code from RegexHero.com :
string strRegex = #"(?<=####.+?:).*?(?=,)";
Regex myRegex = new Regex(strRegex);
string strTargetString = #" ""{\""`####`Answer_Options11\"": \""monkey22\"",\""`####`Answer_Options\"": \""monkey\"",\""Answer_Options2\"": \""not a monkey\""}""";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
this will match "monkey22" and "monkey" but not "not a monkey"
Working from #Jonesy's answer I got to this which works for what I wanted. It includes the .Replace on the groups that I required. The negative look ahead and behinds were very interesting but I needed to replace some of those values hence groups.
static public string MatchKey(string input)
{
string strRegex = #"(__u__)(.+?:\s*)""(.*)""(,|})*";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
IQS_Encryption.Encryption enc = new Encryption();
int count = 1;
string addedJson = "";
int matchCount = 0;
foreach (Match myMatch in myRegex.Matches(input))
{
if (myMatch.Success)
{
//Console.WriteLine("REGEX MYMATCH: " + myMatch.Value);
input = input.Replace(myMatch.Value, "__e__" + myMatch.Groups[2].Value + "\"c" + count + "\"" + myMatch.Groups[4].Value);
addedJson += "c"+count + "{" +enc.EncryptString(myMatch.Groups[3].Value, Encoding.UTF8.GetBytes("12345678912365478912365478965412"))+"},";
}
count++;
matchCount++;
}
Console.WriteLine("MAC" + matchCount);
return input + addedJson;
}`
Thanks again to #Jonesy for the huge help.

Extract some numbers and decimals from a string

I have a string:
" a.1.2.3 #4567 "
and I want to reduce that to just "1.2.3".
Currently using Substring() and Remove(), but that breaks if there ends up being more numbers after the pound sign.
What's the best way to go about doing this? I've read a bunch of questions on regex & string.split, but I can't get anything I try to work in VB.net. Would I have to do a match then replace using the match result?
Any help would be much appreciated.
This should work:
string input = " a.1.2.3 #4567 ";
int poundIndex = input.IndexOf("#");
if(poundIndex >= 0)
{
string relevantPart = input.Substring(0, poundIndex).Trim();
IEnumerable<Char> numPart = relevantPart.SkipWhile(c => !Char.IsDigit(c));
string result = new string(numPart.ToArray());
}
Demo
Try this...
String[] splited = split("#");
String output = splited[0].subString(2); // 1 is the index of the "." after "a" considering there are no blank spaces before it..
Here is regex way of doing it
string input = " a.1.2.3 #4567 ";
Regex regex = new Regex(#"(\d\.)+\d");
var match = regex.Match(input);
if(match.Success)
{
string output = match.Groups[0].Value;//"1.2.3"
//Or
string output = match.Value;//"1.2.3"
}
If the pound sign is the most relevant bit, rely on Split. Sample VB.NET code:
Dim inputString As String = " a.1.2.3 #4567 "
If (inputString.Contains("#")) Then
Dim firstBit As String = inputString.Split("#")(0).Trim()
Dim headingToRemove As String = "a."
Dim result As String = firstBit.Substring(headingToRemove.Length, firstBit.Length - headingToRemove.Length)
End If
As far as this is a multi-language question, here comes the translation to C#:
string inputString = " a.1.2.3 #4567 ";
if (inputString.Contains("#"))
{
string firstBit = inputString.Split('#')[0].Trim();
string headingToRemove = "a.";
string result = firstBit.Substring(headingToRemove.Length, firstBit.Length - headingToRemove.Length);
}
I guess another way using unrolled
\d+ (?: \. \d+ )+

How to replace email address with name in text using regular expressions in C#?

how can I replace the email addresses in a paragraph assuming it's a string now, with names ?
like xx#yahoo.com.my = xx , .com, .ae
Input = "contact abc#yahoo.com or defg#hotmail.eu for more details"
Output = "contact Abc or Defg for more details"
Since you're asking for a Regex, I'm going to give you one.
Regex regex = new Regex(#"(\.|[a-z]|[A-Z]|[0-9])*#(\.|[a-z]|[A-Z]|[0-9])*");
foreach (Match match in regex.Matches(inputString))
{
// match.Value == "xx#yahoo.com.my"
string name = match.Groups[1]; // "xx"
string domain = match.Groups[2]; // "yahoo.com.my"
}
int end = myString.IndexOf('#');
string name=myString.Substring(0, end);
Try like this.
You can refer substring function here>>
http://www.dotnetperls.com/substring
Sting input = "contact abc#yahoo.com or defg#hotmail.eu for more details";
String pattern = #"(\S*)#\S*\.\S*";
String result = Regex.Replace(input , pattern , "$1");
public static string ReplaceEmail(string emailBody) {
string scrubbedemailBody = emailBody;
Regex regex = new Regex(#"(\.|[a-z]|[A-Z]|[0-9])*#(\.|[a-z]|[A-Z]|[0-9])*");
scrubbedemailBody = regex.Replace(scrubbedemailBody, match => {
return new string(' ', match.Length);
});
return scrubbedemailBody;
}

Categories

Resources