Replacing string between Specific String - c#

How to replace string between some Specific String in c#
For Exmaple
string temp = "I love ***apple***";
I need to get value between "***" string, i.e. "apple";
I have tried with IndexOf, but only get first index of selected value.

You should use regex for proper operation of various values like ***banana*** or ***nut*** so the code below may useful for your need. I created for both replacement and extraction of values between *** ***
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace RegexReplaceTest
{
class Program
{
static void Main(string[] args)
{
string temp = "I love ***apple***, and also I love ***banana***.";
//This is for replacing the values with specific value.
string result = Regex.Replace(temp, #"\*\*\*[a-z]*\*\*\*", "Replacement", RegexOptions.IgnoreCase);
Console.WriteLine("Replacement output:");
Console.WriteLine(result);
//This is for extracting the values
Regex matchValues = new Regex(#"\*\*\*([a-z]*)\*\*\*", RegexOptions.IgnoreCase);
MatchCollection matches = matchValues.Matches(temp);
List<string> matchResult = new List<string>();
foreach (Match match in matches)
{
matchResult.Add(match.Value);
}
Console.WriteLine("Values with *s:");
Console.WriteLine(string.Join(",", matchResult));
Console.WriteLine("Values without *s:");
Console.WriteLine(string.Join(",", matchResult.Select(x => x.Trim('*'))));
}
}
}
And a working example is here: http://ideone.com/FpKaMA
Hope this examples helps you with your issue.

Related

Regex - Extract string patterns

I have many strings like these
/test/v1/3908643GASF/item
/test/v1/343569/item/AAAS45663/document
/test/v2/field/1230FRE/item
...
For each one I need to extract the defined pattern like these
/test/v1/{Value}/item
/test/v1/{Value}/item/{Value}/document
/test/v2/field/{Value}/item
The value can be a guid or something else, Can I match the given string patterns with input paths with regex?
I wrote just this code but I don't konw how to match input paths with patterns. The result should be the pattern. Thank you
string pattern1 = "/test/v1/{Value}/item";
string pattern2 = "/test/v1/{Value}/item/{Value}/document";
string pattern3 = "/test/v2/field/{Value}/item";
List<string> paths = new List<string>();
List<string> matched = new List<string>();
paths.Add("/test/v1/3908643GASF/item");
paths.Add("/test/v1/343569/item/AAAS45663/document");
paths.Add("/test/v1/343569/item/AAAS45664/document");
paths.Add("/test/v1/123444/item/AAAS45688/document");
paths.Add("/test/v2/field/1230FRE/item");
foreach (var path in paths)
{
}
This can also be achieved using regex alone. You can probably try:
(\w+)\/\w+(?<=\/item)(\/(\w+)\/)?
Explanation of the above regex:
(\w+) - Represents a capturing group matching a word character one or more time. This group captures our required result.
\/\w+(?<=\/item) - Represents a positive look-behind matching the characters before \items.
$1 - Captured group 1 contains the required information you're expecting.
(\/(\w+)\/)? - Represents the second and third capturing group capturing if after item some other values is present or not.
You can find the demo of the above regex in here.
Sample implementation in C#:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(\w+)\/\w+(?<=\/item)(\/(\w+)\/)?";
string input = #"/test/v1/3908643GASF/item
/test/v1/343569/item/AAAS45663/document
/test/v2/field/1230FRE/item";
foreach (Match m in Regex.Matches(input, pattern))
{
Console.Write(m.Groups[1].Value + " ");
if(m.Groups[3].Value != null)
Console.WriteLine(m.Groups[3].Value);
}
}
}
You can find the sample run of the above implementation in here.

C# Regex extract value between string multiple occurrences

I have a text like this
#-cmd1-# Hakona Matata #-cmd2-#
I want to get all values that is like this #-TEXT-#
Here's the code I use, but it works only when there's one occurrence
var text = "#-adsfree-# hakona matata #-adsbottom-#";
Regex regex = new Regex("#-(.*)-#");
var v = regex.Match(text);
string s = v.Groups[1].ToString();
I'm guessing that you might be designing an expression, maybe similar to:
(?<=#-)\b(\w+)\b(?=-#)
Test
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(?<=#-)\b(\w+)\b(?=-#)";
string input = #"#-adsfree-# hakona matata #-adsbottom-#";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.

C# "between strings" run several times

Here is my code to find a string between { }:
var text = "Hello this is a {Testvar}...";
int tagFrom = text.IndexOf("{") + "{".Length;
int tagTo = text.LastIndexOf("}");
String tagResult = text.Substring(tagFrom, tagTo - tagFrom);
tagResult Output: Testvar
This only works for one time use.
How can I apply this for several Tags? (eg in a While loop)
For example:
var text = "Hello this is a {Testvar}... and we have more {Tagvar} in this string {Endvar}.";
tagResult[] Output (eg Array): Testvar, Tagvar, Endvar
IndexOf() has another overload that takes the start index of which starts to search the given string. if you omit it, it will always look from the beginning and will always find the first one.
var text = "Hello this is a {Testvar}...";
int start = 0, end = -1;
List<string> results = new List<string>();
while(true)
{
start = text.IndexOf("{", start) + 1;
if(start != 0)
end = text.IndexOf("}", start);
else
break;
if(end==-1) break;
results.Add(text.Substring(start, end - start));
start = end + 1;
}
I strongly recommend using regular expressions for the task.
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var regex = new Regex(#"(\{(?<var>\w*)\})+", RegexOptions.IgnoreCase);
var text = "Hello this is a {Testvar}... and we have more {Tagvar} in this string {Endvar}.";
var matches = regex.Matches(text);
foreach (Match match in matches)
{
var variable = match.Groups["var"];
Console.WriteLine($"Found {variable.Value} from position {variable.Index} to {variable.Index + variable.Length}");
}
}
}
}
Output:
Found Testvar from position 17 to 24
Found Tagvar from position 47 to 53
Found Endvar from position 71 to 77
For more information about regular expression visit the MSDN reference page:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
and this tool may be great to start testing your own expressions:
http://regexstorm.net/tester
Hope this help!
I would use Regex pattern {(\\w+)} to get the value.
Regex reg = new Regex("{(\\w+)}");
var text = "Hello this is a {Testvar}... and we have more {Tagvar} in this string {Endvar}.";
string[] tagResult = reg.Matches(text)
.Cast<Match>()
.Select(match => match.Groups[1].Value).ToArray();
foreach (var item in tagResult)
{
Console.WriteLine(item);
}
c# online
Result
Testvar
Tagvar
Endvar
Many ways to skin this cat, here are a few:
Split it on { then loop through, splitting each result on } and taking element 0 each time
Split on { or } then loop through taking only odd numbered elements
Adjust your existing logic so you use IndexOf twice (instead of lastindexof). When you’re looking for a } pass the index of the { as the start index of the search
This is so easy by using Regular Expressions just by using a simple pattern like {([\d\w]+)}.
See the example below:-
using System.Text.RegularExpressions;
...
MatchCollection matches = Regex.Matches("Hello this is a {Testvar}... and we have more {Tagvar} in this string {Endvar}.", #"{([\d\w]+)}");
foreach(Match match in matches){
Console.WriteLine("match : {0}, index : {1}", match.Groups[1], match.index);
}
It can find any series of letters or number in these brackets one by one.

How to pass values from a string array into a Regex in C#

I am currently writing a program that will check a line from a file and see if a US state is contained in that line and that it is also spelled correctly. I have it currently working for a single state. I would like to be able to see if any of all the US states are in the line.
This is my code so far below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
var r = new Regex(#"\bArizona\b", RegexOptions.IgnoreCase);
string[] lines = File.ReadAllLines(#"C:\sampledata.dat");
foreach (string s in lines)
{
var m = r.Match(s);
Console.WriteLine(m.Success); // false
Console.WriteLine(s);
Console.ReadLine();
}
}
}
}
Essentially I would like to do something like
var r = new Regex(#"\bAll US States.txt\b", RegexOptions.IgnoreCase);
If you wanted to see if any of the states were contained, you could essentially use the string.Join() method to generate an expression that would match any of them:
// Read your lines
var states = File.ReadAllLines(#"C:\states.txt");
// Build a pipe delimited string (e.g. State1|State2|State3 ...) to use an a Regex with necessary boundaries
// leading and trailing boundaries
var pattern = $#"\b{(string.Join("\b|\b", states))}\b";
// Now use that pattern to build a Regex to check against (using C# string interpolation)
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
// Now loop through your data here and check each line
string[] lines = File.ReadAllLines(#"C:\sampledata.dat");
foreach (string s in lines)
{
var m = regex.Match(s);
Console.WriteLine(m.Success); // false
Console.WriteLine(s);
Console.ReadLine();
}
Additionally, if you aren't able to use string interpolation to build your pattern, simply use the older string.Format() approach:
var pattern = string.Format("\b{0}\b", string.Join("\b|\b", states));
The regex you're looking for is
\b(?:Alabama|Alaska|Arizona|...)\b
To compose it from a list stored in an All US States.txt file use File.ReadAllLines:
var states = File.ReadAllLines(#"All US States.txt");
var r = new Regex(string.Format(#"\b(?:{0})\b", string.Join("|", states)), RegexOptions.IgnoreCase);

Regex pattern incorrect and combination

I have regex pattern like below:
Regex rx1 = new Regex(#"<div>/\*(.(?!\*/))*\*/(</div>|<br/></div>|<br></div>)");
Regex rx2 = new Regex(#"/\*[^>]+?\*/(<br/>|<br>)");
Regex rx3 = new Regex(#"/\*[^>]+?\*/");
Can anybody help to join together the regexes become 1 pattern?
Your problem with RX1 is because of (.(?!\*/))*\*/ which captures any character zero or more times aslong as it is not followed by */ because of this the answer can never match.
UPDATED Answer
#"(?'div'<div>)?/\*((?<!\*/).)*?\*/(?:<br/?>)?(?'-div'</div>)?(?(div)(?!))"
This will capture:
(?'div'<div>) an optional opening div stored in capture group div
/\* char sequence /*
((<!\*/).)*? zero or more characters, non greedy and each character is not
preceded by the string */
\*/ char sequence `*/`
(?:<br/?>)? optionally <br> or <br/>
(?'-div'</div>)? optionally </div> remove from capture group `div`
(?(div)(?!)) match only if capture group div is empty (ie balanced <div> </div>)
I think you need this for combining the patterns:
(pattern1|pattern2|pattern3) means pattern1 or pattern2 or pattern3
Try the following(It's frankenstein code but it helps you manage each regex variable as it's own as opposed to concatenating all three into one big regex(although that it is not wrong but it can be hard to manage changes to the regex).:
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace BatchRegex
{
class Program
{
static void Main(string[] args)
{
string[] target =
{
"<div>/*...*/</div> <div>/*...*/<br></div> <div>/*...*/<br></div>",
"/*...*/<br></div> or /*...*/<br/></div>"
};
foreach (var tgt in target)
{
var rx1 = new Regex[]{new Regex(#"<div>/\*(.(?!\*/))*\*/(</div>|<br/></div>|<br></div>)", RegexOptions.Multiline),
new Regex(#"/\*[^>]+?\*/(<br/>|<br>)", RegexOptions.Multiline),
new Regex(#"/\*[^>]+?\*/", RegexOptions.Multiline)};
foreach (var rgx in rx1)
{
var rgxMatches = rgx.Matches(tgt).Cast<Match>();
Parallel.ForEach(rgxMatches, match =>
{
Console.WriteLine("Found {0} in target {1}.", match, tgt);
});
}
}
Console.Write("Press any key to exit...");
Console.ReadKey();
}
}
}

Categories