This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 4 years ago.
This is probably a duplicate but I m too dense to make this work. I m trying to learn capture groups etc but I cant make this one work, I get the right matches but only the last digit gets matched.
class Program
{
static void Main(string[] args)
{
string testTxt = "sadsadas168.asdsad";
string asdf = "wrongwrong";
string abc = "abc.png";
string bsdf = "100";
string lolol = "155abx.text";
string another = "sasd199.ong100";
TryMatch(testTxt);
TryMatch(asdf);
TryMatch(abc);
TryMatch(bsdf);
TryMatch(lolol);
TryMatch(another);
Console.ReadLine();
}
private static void TryMatch(string txt)
{
var rgx = new Regex(#"\w*(?<t>\d+)\w*\..*");
var m = rgx.Match(txt);
if (m.Success)
{
int mainID = int.Parse(m.Groups["t"].Value);
Console.WriteLine(m.Groups["t"].Value);
}
else
{
Console.WriteLine("Did not match " + txt);
}
}
}
Related
This question already has answers here:
How can I uppercase the first letter of all words in my string?
(9 answers)
Split a string by another string in C#
(11 answers)
Closed 10 months ago.
I am writing C# codes for Tabular Editor. I have a string Like "mkt_tag" and want to convert it into "Mkt Tag". I wrote this code but still have some problems. Please help me to out.
foreach(var obj in Selected.Columns) {
var oldName = obj.Name; //"mkt_tag"
var newName = new System.Text.StringBuilder();
for(int i = 0; i < oldName.Length; i++) {
// First letter should always be capitalized:
if(i == 0) newName.Append(Char.ToUpper(oldName[i]));
else if(oldName[i] == '_')
{
newName.Append(" ");
newName.Append(Char.ToUpper(oldName[i+1]));
}
else
{
newName.Append(oldName[i]);
}
}
obj.Name = newName.ToString();
newName.Output();
}
Below is the current output:- "Mkt Ttag"
enter image description here
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 3 years ago.
I am trying to make a swear word filter in c#. I am using a string list and going through it and checking if the input contains a word from the list. But when i run the code, it does not filter anything.
I have tried to make it an array change it to a boolean but nothing seems to work.
private List<string> badWords = new List<string>();
public string FilterSwear(string text)
{
string filterd = text;
foreach (string badWord in badWords)
{
if (text.Contains(badWord))
{
int chars = badWord.Length;
string stars = "";
for (int i = 0; i < chars; i++)
{
stars += "*";
}
filterd.Replace(badWord, stars);
}
}
return filterd;
}
Try:
filterd = filterd.Replace(badWord, stars);
Replace doesn't replace in-place - it returns copy with replaced string and leaves original intact.
This question already has an answer here:
C# Remove a letter that trails a number from multiple instances in a string whether with Regex by another method
(1 answer)
Closed 4 years ago.
Suppose I have a file like this:
EN;05;UK;55;EN;66;US;87;US;89;EN;66;UK;87;
I want remove all the EN occurrence, so the final string should be:
UK;55;US;87;US;89;UK;87;
I can remove the EN using string.Replace("EN", "") but how to remove also the number?
I will propose an alternative not using RegEx. This will work if the number following the abbreviation is more than two characters.
public static string RemoveDataPoints(this string data, string indentifier)
{
var temp = "";
var fields = data.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for (int i=0; i<fields.Length; i=i+2)
{
if (!fields[i].Equals(indentifier))
{
temp += String.Format("{0};{1};", fields[i], fields[i + 1]);
}
}
return temp;
}
Then call:
var output = "EN;05;UK;55;EN;66;US;87;US;89;EN;66;UK;87;".RemoveDataPoints("EN");
You could make use of a regular expression:
string output = Regex.Replace(input, "EN\\;\\d{2}\\;", "");
where input is your string, EN;05;UK;55;EN;66;US;87;US;89;EN;66;UK;87;.
This question already has answers here:
C# replace string in string
(7 answers)
Closed 4 years ago.
I have a function in where I want to convert a string value C:\samplec#programs\Converter to C:\\samplec#programs\\Converter Note the differences. This is my function:
private string FilePathProcessor(string path)
{
char[] oriCharArray = path.ToCharArray;
List<char> oriCharList = new List<char>;
List<char> newCharList = new List<char>;
foreach (char item in oriCharArray)
{
oriCharList.Add(item);
}
foreach (char items in oriCharList)
{
if ((items != "\\"))
{
newCharList.Add(items);
}
else
{
newCharList.Add(items);
newCharList.Add(items);
}
}
string result = string.Join(",", newCharList.ToArray());
return result;
}
Of course this function serves my needs. But, I wonder if there is already an existing function in .Net which will take care of it. I am just cleaning up my code and checking for simpler and faster solution. Not going to reinvent the wheel if there is already a way.
Use String.Replace()
string path = #"C:\samplec#programs\Converter";
string output = path.Replace("\\", #"\\");
This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
Closed 6 years ago.
I want to check whether string below contains top/ TOP/toP/ Top/TOp/ Top in c#. My code is like
string str = null;
str = "CSharp Top11111 10 BOOKS";
if (str.Contains("top") == true)
{
Console.WriteLine("The string Contains() 'TOP' ");
}
else
{
Console.WriteLine("The String does not Contains() 'TOP'");
}
But it return true only when my string contain 'top'. How can return true for all other scenarios too? I know this may be simple, but I searched a Lot didn't find any solutions
Without the need for any conversion:
bool found = "My Name is".IndexOf("name", StringComparison.OrdinalIgnoreCase) >= 0;
Use one of both: .ToLower() or .ToUpper
string str = null;
str = "CSharp Top11111 10 BOOKS";
if (str.ToLower().Contains("top") == true)
{
Console.WriteLine("The string Contains() 'TOP' ");
}
else
{
Console.WriteLine("The String does not Contains() 'TOP'");
}