I have some strings like
string text =
"these all are strings, 000_00_0 and more strings are there I have here 649_17_8, and more with this format 975_63_7."
So here I wanted to read only 000_00_0, 649_17_8, 975_64_7... All the string with this format.
Please help me with the situation
You can use Regex class and its Matches method.
var matches = System.Text.RegularExpressions.Regex.Matches(input, "([_0-9]+)");
var numberishs1 = matches
.Select(m => m.Groups[1].ToString())
.ToList();
var s1 = string.Join(", ", numberishs1);
// .NETCoreApp,Version=v3.0
these all are strings, 000_00_0 and more strings are there I have here 649_17_8, and more with this format 975_63_7.
000_00_0, 649_17_8, 975_63_7
You have to use regular expression.
In your case you want to find the pattern "..._.._."
If you only want numbers it would be pattern "([0-9]{3}_[0-9]{2}_[0-9]{1})"
Try this:
using System;
using System.Text.RegularExpressions;
namespace SyntaxTest
{
class Program
{
static void Main(string[] args)
{
var input =
"these all are strings, 000_00_0 and more strings are there I have here 649_17_8, and more with this format 975_63_7.";
var pattern = "..._.._.";
Match result = Regex.Match(input, pattern);
if (result.Success)
{
while (result.Success)
{
Console.WriteLine("Match: {0}", result.Value);
result = result.NextMatch();
}
Console.ReadLine();
}
}
}
}
Related
Need to replace operands named as [WORD, WORD1, WORD2,..., WORDnnn] in an expression like:
WORD-WORD1+WORD11
with operands named as:
[WORD_NEW, WORD1_NEW, WORD2_NEW, WORDnnn_NEW]
Some of the operands are not mapped, and those should not be replaced.
WORD-WORD1+WORD11 => WORD_NEW-WORD1_NEW+WORD11_NEW
WORD-WORD1+WORD11 => WORD_NEW-WORD1_NEW+WORD11 if WORD11 is not mapped.
Since you already have map (presumably in form of Dictionary<string,string>) just run Replace that takes delegate and check if mapping is present for each particular match:
var mapping = new Dictionary<string,string>{{"WORD1", "WORD_NEW1"}};
var result = Regex.Replace("WORD-WORD1+WORD11", "WORD\d+",
match => mapping.ContainsKey(match.Value)? mapping[match.Value] : match.Value);
// result is "WORD-WORD_NEW1+WORD11"
This Should Work.
Regexp:
(WORD\d*)
Replace with:
$1_NEW
C# Code:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(WORD\d*)";
string substitution = #"$1_NEW";
string input = #"WORD, WORD1, WORD2";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
Console.WriteLine(result);
}
}
OUTPUT:
WORD_NEW, WORD1_NEW, WORD2_NEW
See: https://regex101.com/r/2uTCjD/1
Test it: http://ideone.com/01Yxng
I am working on solution where I need to validate and pass only valid characters of string in c#.
E.g. my regular expression is : "^\\S(|(.|\\n)*\\S)\\Z"
and text I want validate is below
127 Finchfield Lane
Now I know its invalid. But how do I remove invalid against regex and pass only if string validate successfully against regex ?
if i understand you correctly, you are looking for Regex.IsMatch
if(Regex.IsMatch(str, "^\\S(|(.|\\n)*\\S)\\Z"))
{
// do something with the valid string
}
else
{
// strip invalid characters from the string
}
using System;
using System.Text.RegularExpressions;
namespace PatternMatching
{
class Program
{
static void Main()
{
string pattern = #"(\d+) (\w+)";
string[] strings = { "123 ABC", "ABC 123", "CD 45678", "57998 DAC" };
foreach (var s in strings)
{
Match result = Regex.Match(s, pattern);
if (result.Success)
{
Console.WriteLine("Match: {0}", result.Value);
}
}
Console.ReadKey();
}
}
}
This seems to do what you require. Hope I haven't misunderstood.
To validate the string against regex you can use use Regex.IsMatch.
Regex.IsMatch(string, pattern) //returns true if string is valid
If you want to get the Match value only then you can use it.
Match match = new Regex(#"\d+").Match(str);
match.value; //it returns only the matched string and unmatched string automatically stripped out
I am using Regex to remove unwanted characters from string like below:
str = System.Text.RegularExpressions.Regex.Replace(str, #"[^\u0020-\u007E]", "");
How can I retrieve distinct characters which will be removed in efficient way?
EDIT:
Sample input : str = "This☺ contains Åüsome æspecialæ characters"
Sample output : str = "This contains some special characters"
removedchar = "☺,Å,ü,æ"
string pattern = #"[\u0020-\u007E]";
Regex rgx = new Regex(pattern);
List<string> matches = new List<string> ();
foreach (Match match in rgx.Matches(str))
{
if (!matches.Contains (match.Value))
{
matches.Add (match.Value);
}
}
Here is an example how you can do it with a callback method inside the Regex.Replace overload with an evaluator:
evaluator
Type: System.Text.RegularExpressions.MatchEvaluator
A custom method that examines each match and returns either the original matched string or a replacement string.
C# demo:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Test
{
public static List<string> characters = new List<string>();
public static void Main()
{
var str = Regex.Replace("§My string 123”˝", "[^\u0020-\u007E]", Repl);//""
Console.WriteLine(str); // => My string 123
Console.WriteLine(string.Join(", ", characters)); // => §, ”, ˝
}
public static string Repl(Match m)
{
characters.Add(m.Value);
return string.Empty;
}
}
See IDEONE demo
In short, declare a "global" variable (a list of strings, here, characters), initialize it. Add the Repl method to handle the replacement, and when Regex.Replace calls that method, add each matched value to the characters list.
I have little experince in C# and regex, but I need try this this logic:
string replacedText = Regex.Replace(
"ssdf bonnets sdf sdf sdf ",
#"(?i)^(.+ )?(bonnet)(s?)( .+)?$",
"$1hood$3$4"
);
The above code was an answer to question in stackoverflow:
Replacing a part of string while keeping the rest intact?
instead of detecting just the word (bonnet) I want to replace multiple values for example if it finds "f" or "b" or "s" it will be replaced by "a"?
for example if the input "ahfbsdrts stb"
the output wll be "ahaaadrta ata"
I post another option for short code.
Please see http://forums.asp.net/t/1185961.aspx/1
Something like string temp = Regex.Replace(input, #"[fbs]", "a");
Why not just use multiple calls to String.Replace?
Try this:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "ssdf bonnets sdf sdf sdf ";
string pattern_1 = "f";
string replacement = "a";
Regex rgx_1 = new Regex(pattern_1);
string result = rgx_1.Replace(input, replacement);
string pattern_2 = "b";
Regex rgx_2 = new Regex(pattern_2);
result = rgx_2.Replace(result, replacement);
string pattern_3 = "s";
Regex rgx_3 = new Regex(pattern_3);
result = rgx_3.Replace(result, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
}
}
Can I do something like following to remove specific strings from the end of the words ?
public static HashSet<string> stringtoremove = new HashSet<string>
...............
.................
public static string stepone(this string word)
{
if (stringtoremove(word.EndsWith))
{
word = ..................................;
}
return word;
}
I tried but it doesn't work. did i miss something in my code ? thanks in advance.
The best option is to use Regular Expressions; have a look at the Replace method.
string input = "test testabc test123 abc abctest";
string pattern = #"(abc\b)";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
I assume that you actually want to look into the HashSet<String> to see if the given string parameter ends with one of these words. If so, remove it from the end of the string.
You can use FirstOrDefault to determine the first string in the set that is also the end of the given word:
var firstMatch = stringtoremove.FirstOrDefault(str => word.EndsWith(str));
if (firstMatch != null)
return word.Substring(0, word.Length - firstMatch.Length);
else
return word;
Why don't you use String.TrimEnd method?
word = word.TrimEnd(charsToTrim)