Why this will print the same result?
string tester = "stUniqueId01";
Debug.WriteLine("ID: " + tester);
var regex = tester.Replace("[^0-9.]", "");
Debug.WriteLine("ID: " + regex);
Output:
ID: stUniqueId01
ID: stUniqueId01
You are calling string.Replace, not Regex.Replace. I think you want:
string tester = "stUniqueId01";
Debug.WriteLine("ID: " + tester);
var regex = new Regex("[^0-9.]");
Debug.WriteLine("ID: " + regex.Replace(tester,""));
or:
string tester = "stUniqueId01";
Debug.WriteLine("ID: " + tester);
var replaced = Regex.Replace(tester,"[^0-9.]","");
Debug.WriteLine("ID: " + replaced);
if you don't intend to reuse the regular expression.
You are using the Replace method from String. It takes strings, not regular expressions. Try:
string tester = "stUniqueId01";
Console.WriteLine("ID: " + tester);
Regex rx = new Regex("[^0-9.]");
var regex = rx.Replace(tester, "");
Console.WriteLine("ID: " + regex);
Result:
ID: stUniqueId01
ID: 01
Related
Starting with an example, I have some keywords like
Narendra Modi, Modi, India, Speech, Parliament
And I have a story with text
Narendra Modi will give a speech in the parliament of India.
Now I want my regex to replace the keyword with the hyperlink in the story.
<a>Narendra Modi</a> will give speech in the <a>parliament</a> of <a>India.</a>
My Code for this is
var tagArray = bodykeywords.Split(',').ToList();
foreach (var tag in tagArray.OrderBy(a => a.Length))
{
var replaceTag = tag.Replace("(", "");
replaceTag = replaceTag.Replace(")", "");
DataDesc = Regex.Replace(DataDesc, "\"" + replaceTag.Trim() + "\"", " \"" + replaceTag.Trim() + "\" ");
DataDesc = Regex.Replace(DataDesc, " " + replaceTag.Trim() + ", ", " " + replaceTag.Trim() + ", ");
DataDesc = Regex.Replace(DataDesc, " " + replaceTag.Trim() + " ", " " + replaceTag.Trim() + " ");
}
Problem is I am not able to replace the word with full stop like India in the given example and Word with repetition like Narendra Modi, Modi in the keyword.
I have string:
string mystring = "hello(hi,mo,wo,ka)";
And i need to get all arguments in brackets.
Like:
hi*mo*wo*ka
I tried that:
string res = "";
string mystring = "hello(hi,mo,wo,ka)";
mystring.Replace("hello", "");
string[] tokens = mystring.Split(',');
string[] tokenz = mystring.Split(')');
foreach (string s in tokens)
{
res += "*" + " " + s +" ";
}
foreach (string z in tokenz)
{
res += "*" + " " + z + " ";
}
return res;
But that returns all words before ",".
(I need to return between
"(" and ","
"," and ","
"," and ")"
)
You can try to use \\(([^)]+)\\) regex get the word contain in brackets,then use Replace function to let , to *
string res = "hello(hi,mo,wo,ka)";
var regex = Regex.Match(res, "\\(([^)]+)\\)");
var result = regex.Groups[1].Value.Replace(',','*');
c# online
Result
hi*mo*wo*ka
This way :
Regex rgx = new Regex(#"\((.*)\)");
var result = rgx.Match("hello(hi,mo,wo,ka)");
Split method has an override that lets you define multiple delimiter chars:
string mystring = "hello(hi,mo,wo,ka)";
var tokens = mystring.Replace("hello", "").Split(new[] { "(",",",")" }, StringSplitOptions.RemoveEmptyEntries);
I am trying to process some text with unusual pattern. The text looks like below:
||Names : XYZ DJ Age : 23 Years Location: New York; end;'
2018-03-20 11:59:59.397, mnx=0x0000700, pid=90c9ac, xSG: dlgID:34
AppDlg:774 params: 2018-03-20 11:59:59.397, mnx=0x700000,
pid=090c9ac, lBG: OPCDManager::Response: 0x7f083 2018-03-20
11:59:59.397, mxn=0x000070, pid=f90c9ac, lBG: DlgID:37774 sess:'990'
conID:1 dlClose:false params:
Now, I want to load this data into a text file as below:
XYZ DJ-23 Years-New York,2018-03-20 11:59:59.397, mnx=0x0000700,
pid=90c9ac, xSG: dlgID:34 AppDlg:774 params: XYZ DJ-23 Years-New
York,2018-03-20 11:59:59.397, mnx=0x700000, pid=090c9ac,
lBG: OPCDManager::Response: 0x7f083 XYZ DJ-23 Years-New
York,2018-03-20 11:59:59.397, mxn=0x000070, pid=f90c9ac,
lBG: DlgID:37774 sess:'990' conID:1 dlClose:false params:
I have tried the below code but it does not give me what I want. Instead, it gives me one long text strings instead of several rows:
string linesc = File.ReadAllText(path);
string[] linesx = linesc.Split('|');
foreach (string s in linesx)
{
string new2=s.Replace(Environment.NewLine, " ");
File.AppendAllText(path2 + "myfile.txt", new2 + Environment.NewLine);
}
How can I modify the code so that I get the rows above?
Try the following:
string linesc = File.ReadAllText(path);
string[] linesx = linesc.Split('|');
foreach (string s in linesx)
{
string new2=s.Replace(Environment.NewLine, " ")
.Replace("Names : ", "")
.Replace("Age : ", "")
.Replace("Location : ", "") + "\n";
File.AppendAllText(path2 + "myfile.txt", new2 + Environment.NewLine);
}
I also took care of removing "Name : ", "Age : " and "Location : " from the input.
You can also try this approach:
string text = #"||Names : XYZ DJ
Age : 23 Years
Location: New York; end;'
2018-03-20 11:59:59.397, mnx=0x0000700, pid=90c9ac, xSG: dlgID:34 AppDlg:774 params:
2018-03-20 11:59:59.397, mnx=0x700000, pid=090c9ac, lBG: OPCDManager::Response: 0x7f083
2018-03-20 11:59:59.397, mxn=0x000070, pid=f90c9ac, lBG: DlgID:37774 sess:'990' conID:1 dlClose:false params:";
StringBuilder result = new StringBuilder();
string[] allLines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string name = allLines[0].Replace("||Names : ", string.Empty).Trim();
string age = allLines[1].Replace("Age : ", string.Empty).Replace("Years", string.Empty).Trim();
string location = allLines[2].Replace("Location: ", string.Empty).Replace("; end;'", string.Empty).Trim();
for(int i = 3; i < allLines.Length; i++)
{
result.AppendLine($"{name}-{location},{allLines[i].Trim()}");
}
string res = result.ToString();
Console.WriteLine(res);
You have to break this down into several steps:
extract the 'global' information from the first three lines
iterate over the listing lines and concatenate them to the output format
At the moment you are splitting by '|' which results in ["", "", "Names : XYZ ... your whole other text"].
So for example (not tested):
string[] linesc = File.ReadAllText(path).Split(new string[]{Environment.NewLine}, StringSplitOptions.None);
// extract global infos
string[] name = linesc[0].Split(':');
string[] age = linesc[1].Split(':');
string[] location = linesc[2].Split(':');
for (int i=3; i<linesc.Length; i++)
{
// reconcatenate new line
string new2 = name[1] + "-" + age[1] + "-" + location[1] + "," + linesc[i];
File.AppendAllText(path2 + "myfile.txt", new2 + Environment.NewLine);
}
If you also want to get rid of leading/trailing spaces you can use Trim() on each of the string parts.
I need to support parsing xml that is inside an email body but with extra text in the beginning and the end.
I've tried the HTML agility pack but this does not remove the non-xml texts.
So how do I cleanse the string w/c contains an entire xml text mixed with other texts around it?
var bodyXmlPart= #"Hi please see below client <?xml version=""1.0"" encoding=""UTF-8""?>" +
"<ac_application>" +
" <primary_applicant_data>" +
" <first_name>Ross</first_name>" +
" <middle_name></middle_name>" +
" <last_name>Geller</last_name>" +
" <ssn>123456789</ssn>" +
" </primary_applicant_data>" +
"</ac_application> thank you, \n john ";
//How do I clean up the body xml part before loading into xml
//This will fail:
var xDoc = XDocument.Parse(bodyXmlPart);
If you mean that body can contain any XML and not just ac_application. You can use the following code:
var bodyXmlPart = #"Hi please see below client " +
"<ac_application>" +
" <primary_applicant_data>" +
" <first_name>Ross</first_name>" +
" <middle_name></middle_name>" +
" <last_name>Geller</last_name>" +
" <ssn>123456789</ssn>" +
" </primary_applicant_data>" +
"</ac_application> thank you, \n john ";
StringBuilder pattern = new StringBuilder();
Regex regex = new Regex(#"<\?xml.*\?>", RegexOptions.Singleline);
var match = regex.Match(bodyXmlPart);
if (match.Success) // There is an xml declaration
{
pattern.Append(#"<\?xml.*");
}
Regex regexFirstTag = new Regex(#"\s*<(\w+:)?(\w+)>", RegexOptions.Singleline);
var match1 = regexFirstTag.Match(bodyXmlPart);
if (match1.Success) // xml has body and we got the first tag
{
pattern.Append(match1.Value.Trim().Replace(">",#"\>" + ".*"));
string firstTag = match1.Value.Trim();
Regex regexFullXmlBody = new Regex(pattern.ToString() + #"<\/" + firstTag.Trim('<','>') + #"\>", RegexOptions.None);
var matchBody = regexFullXmlBody.Match(bodyXmlPart);
if (matchBody.Success)
{
string xml = matchBody.Value;
}
}
This code can extract any XML and not just ac_application.
Assumptions are, that the body will always contain XML declaration tag.
This code will look for XML declaration tag and then find first tag immediately following it. This first tag will be treated as root tag to extract entire xml.
I'd probably do something like this...
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Test {
class Program {
static void Main(string[] args) {
var bodyXmlPart = #"Hi please see below client <?xml version=""1.0"" encoding=""UTF-8""?>" +
"<ac_application>" +
" <primary_applicant_data>" +
" <first_name>Ross</first_name>" +
" <middle_name></middle_name>" +
" <last_name>Geller</last_name>" +
" <ssn>123456789</ssn>" +
" </primary_applicant_data>" +
"</ac_application> thank you, \n john ";
Regex regex = new Regex(#"(?<pre>.*)(?<xml>\<\?xml.*</ac_application\>)(?<post>.*)", RegexOptions.Singleline);
var match = regex.Match(bodyXmlPart);
if (match.Success) {
Debug.WriteLine($"pre={match.Groups["pre"].Value}");
Debug.WriteLine($"xml={match.Groups["xml"].Value}");
Debug.WriteLine($"post={match.Groups["post"].Value}");
}
}
}
}
This outputs...
pre=Hi please see below client
xml=<?xml version="1.0" encoding="UTF-8"?><ac_application> <primary_applicant_data> <first_name>Ross</first_name> <middle_name></middle_name> <last_name>Geller</last_name> <ssn>123456789</ssn> </primary_applicant_data></ac_application>
post= thank you,
john
I've been trying to find a regex pattern to replace all youtube URLs in a string with the iframe embed code (C#). Obviously the video ID has to extracted. Here is the url patterns that should match:
http://www.youtube.com/watch?v=bSiDLCf5u3s
https://www.youtube.com/watch?v=bSiDLCf5u3s
http://youtu.be/bSiDLCf5u3s
www.youtube.com/watch?v=bSiDLCf5u3s
youtu.be/bSiDLCf5u3s
http://www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s
www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s
all possible urls should be replaced with:
<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/VIDEO_ID_EXTRACTED' frameborder='0' allowfullscreen='1'></iframe>
Can someone please point me to a right direction.
Thank you in advance
Here is the regex:
(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)
Should match all the links you posted and extracts the video ID as $1. And with the following code you replace the links with the <iframe/>:
const string input = "http://www.youtube.com/watch?v=bSiDLCf5u3s " +
"https://www.youtube.com/watch?v=bSiDLCf5u3s " +
"http://youtu.be/bSiDLCf5u3s " +
"www.youtube.com/watch?v=bSiDLCf5u3s " +
"youtu.be/bSiDLCf5u3s " +
"http://www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s " +
"www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s " +
"http://www.youtube.com/watch?v=_-QpUDvTdNY";
const string pattern = #"(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)";
const string replacement = "<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/$1' frameborder='0' allowfullscreen='1'></iframe>";
var rgx = new Regex(pattern);
var result = rgx.Replace(input, replacement);
// result ==
// <iframe title='YouTube video player' width='480' height='390' src='https://www.youtube.com/embed/bSiDLCf5u3s' frameborder='0' allowfullscreen='1'></iframe>
// ...
//You can try this:
string strRegex = #"(?<EMBED>(<iframe|<object).*?src=[""'](?<SRC>(http:|https:)?//(www.)?[youtube\.com|youtu.be][^""']+).*?(</iframe>|</object>))";
Regex myRegex = new Regex(strRegex, RegexOptions.Singleline);
string strTargetString = #"<div align=""center""><iframe height=""315"" src=""//www.youtube.com/embed/NiCZAnmjYZ8"" frameborder=""0"" width=""560"" allowfullscreen=""true""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""//youtube.com/embed/NiCZAnmjYZ81"" frameborder=""0"" width=""570"" allowfullscreen=""""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""http://www.youtube.com/embed/NiCZAnmjYZ82"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""https://youtube.com/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""https://youtu.be/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + #"<div align=""center""><iframe height=""315"" src=""http://youtu.be/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + #"<a href=""https://youtu.be/embed/NiCZAnmjYZ83"">Youtube<a>" + "\n" + #"<div style=""text-align:center""><object width=""100%"" height=""100%"" id=""PlayerAS039128cb43804eb7894cba4e8b0220fc"" classid=""clsid:D27CDB6E-AE6D-11cf-96B8-444553540000""><param name=""movie"" value=""http://youtu.be/embed/NiCZAnmjYZ83""></param><param name=""allowFullScreen"" value=""true""></param><param name=""allowscriptaccess"" value=""always""></param><param value=""#000000"" name=""bgcolor""></param><param name=""wmode"" value=""opaque""></param><embed height=""100%"" width=""100%"" quality=""high"" bgcolor=""#000000"" flashvars="""" wmode=""opaque"" allowfullscreen=""true"" allowscriptaccess=""always"" name=""039128cb43804eb7894cba4e8b0220fc"" id=""039128cb43804eb7894cba4e8b0220fc"" style="""" src=""http://youtu.be/embed/NiCZAnmjYZ83"" type=""application/x-shockwave-flash""/></embed></object></div>" + "\n";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}