I have two subsequently repeating texts in "<>". The text is dynamic and has the following pattern:
"some text <text1> <text2> some text"
Based on the condition I need to remove either first or second text in "<>". I also need to remove any brackets.
Example:
"The company <is> <is not> a co-owner of other accounts in the Bank."
if true condition:
"The company is a co-owner of other accounts in the Bank."
if false condition:
"The company is not a co-owner of other accounts in the Bank."
I'd appreciate your help with regex pattern.
var isTrue = true;
var str = "The company <is> <is not> a co-owner of other accounts in the Bank.";
var segment = Regex.Match(str, #"<(.*?)>\W<(.*?)>");
var replacement = str.Replace(segment.Value, isTrue ? segment.Groups[1].Value : segment.Groups[2].Value);
I've to make some prerequisites: some text doesn't contain a matching pair of "<>" brackets.
You can use this code to do your job:
var input = #"some text <truestring> <falsestring> some text";
var replacement = match => <some condition> ? match.Groups["truestring"].Value : match.Groups["falsestring"].Value
var regex = new Regex(#"(?:(?:(?<topen>\<)[^\<\>]*)+(?:(?<truestring-topen>\>)(?(topen)[^\<\>]*))+)+(?(topen)(?!))\s*(?:(?:(?<fopen>\<)[^\<\>]*)+(?:(?<falsestring-fopen>\>)(?(fopen)[^\<\>]*))+)+(?(fopen)(?!))");
var result = regex.Replace(input, replacement);
Replace some condition with your condition. This regex uses BCD to find the matching bracket of truestring or falsestring so truestring and falsestring are able to contain matching backets.
Try with this example:
bool condition = true;
string str = (condition) ? "$2" : "$5";
var text = "some text <trueString> <falseString> some text";
var result = Regex.Replace(text, "(<)([^<>]*)(>).*?(<)([^<>]*)(>)", str);
result will be: some text trueString some text
One more way:
public static string Substitute(string input, bool condition)
{
return Regex.Replace(input, #"<(?<true>.*?)>\s*<(?<false>.*?)>", m => condition ? m.Groups["true"].Value : m.Groups["false"].Value);
}
usage:
string input = "The company <is> <is not> a co-owner of other accounts in the Bank.";
string output = Substitute(input, false);
Use Regex
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = "some text <trueString> <falseString> some text";
string pattern = #"(?'beginning'[^\<]+)\<(?'true'[^\>]+)\>\s*<(?'false'[^\>]+)\>(?'ending'[^$]*)";
Match match = Regex.Match(input, pattern);
bool condition = true;
Regex expr = new Regex(pattern);
string output = "";
if (condition)
{
output = expr.Replace(input, "${beginning}${true}${ending}");
}
else
{
output = expr.Replace(input, "${beginning}${false}${ending}");
}
}
}
}
Related
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();
}
}
}
}
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 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)