How to find out if regexp parsed string part contains another string? - c#

Say we have a list of strings L, a given string S. We have a regexp like (\w+)\-(\w+) we want to get all L elements for which S matches $1 of regexp. How to do such thing?

You can do this:
// sample data
string[] L = new string[] { "bar foo", "foo bar-zoo", "bar-", "zoo bar-foo" };
string S = "bar";
Regex regex = new Regex(#"(\w+)\-(\w+)");
string[] res = L.Where(l => {
Match m = regex.Match(l);
if (m.Success) return m.Groups[1].Value == S;
else return false;
}).ToArray();
and get
foo bar-zoo
zoo bar-foo
An easier way that probably works out for you too is to include S in the regex:
Regex regex = new Regex(S + #"\-(\w+)");
string[] res = L.Where(l => regex.Match(l).Success).ToArray();

Related

RegEx Expression for Parenthesis Matching

I need some help coming up with a C# regular expression that can take a string like:
string input = "Test1='1' OR (Test2 = '2' OR (Test3 = '3')) OR (Test4 = '4')";
and return match items like:
1 => Test1='1' OR
2 => (Test2 = '2' OR (Test3 = '3'))
3 => OR
4 => (Test4 = '4')
Look into Grouping Constructs in Regular Expressions
To get you started.. here's a sample for the input you provided.
string pattern = #"(Test1='1' OR) \(Test2 = '2'";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
foreach(Group group in match.Groups)
{
Console.WriteLine(group);
}
}
Console.ReadKey();

How to find 1 in my string but ignore -1 C#

I have a string
string test1 = "255\r\n\r\n0\r\n\r\n-1\r\n\r\n255\r\n\r\n1\r";
I want to find all the 1's in my string but not the -1's. So in my string there is only one 1. I use string.Contain("1") but this will find two 1's. So how do i do this?
You can use regular expression:
string test1 = "255\r\n\r\n0\r\n\r\n-1\r\n\r\n255\r\n\r\n1\r";
// if at least one "1", but not "-1"
if (Regex.IsMatch(test1, "(?<!-)1")) {
...
}
the pattern is exactly 1 which is not preceed by -. To find all the 1s:
var matches = Regex
.Matches(test1, "(?<!-)1")
.OfType<Match>()
.ToArray(); // if you want an array
Try this simple solution:
Note : You can convert this to extension Method Easily.
static List<int> FindIndexSpecial(string search, char find, char ignoreIfPreceededBy)
{
// Map each Character with its Index in the String
var characterIndexMapping = search.Select((x, y) => new { character = x, index = y }).ToList();
// Check the Indexes of the excluded Character
var excludeIndexes = characterIndexMapping.Where(x => x.character == ignoreIfPreceededBy).Select(x => x.index).ToList();
// Return only Indexes who match the 'find' and are not preceeded by the excluded character
return (from t in characterIndexMapping
where t.character == find && !excludeIndexes.Contains(t.index - 1)
select t.index).ToList();
}
Usage :
static void Main(string[] args)
{
string test1 = "255\r\n\r\n0\r\n\r\n-1\r\n\r\n255\r\n\r\n1\r";
var matches = FindIndexSpecial(test1, '1', '-');
foreach (int index in matches)
{
Console.WriteLine(index);
}
Console.ReadKey();
}
You could use String.Split and Enumerable.Contains or Enumerable.Where:
string[] lines = test1.Split(new[] {Environment.NewLine, "\r"}, StringSplitOptions.RemoveEmptyEntries);
bool contains1 = lines.Contains("1");
string[] allOnes = lines.Where(l => l == "1").ToArray();
String.Contains searches for sub-strings in a given string instance. Enumerable.Contains looks if there's at least one string in the string[] which equals it.

Substring Specific Word Containing Special Character between them

I have following String
string test = "viv-ek is a good boy.Mah - esh is Cra - zy.";
I want to get {"Vivek","Mahesh","Crazy"} words from that string
Some having only "-" and some having " - " in between words.
You can find your words with following regex :
\b\w+(?:\s-\s|-)\w+\b
and replace the result of match strings with (?:\s-\s|-) with empty string ''.
\b\w+\s*-\s*\w+\b
You can try this.See demo.
https://regex101.com/r/cZ0sD2/14
This might do the trick for you
string test = "viv-ek is a good boy.Mah - esh is Cra - zy.";
test = test.Replace(" -", "-").Replace("- ", "-").Replace(".", ". ");
//Or
//test = test.Replace(" - ", "-").Replace(".", ". ");
string[] allwords = test.Split(' ');
List<string> extractedWords=new List<string>();
foreach(string wrd in allwords)
{
if(wrd.Contains("-"))
{
extractedWords.Add(wrd.Replace("-", ""));
}
}
If you only want to select those words use this:
string test = "viv-ek is a good boy.Mah - esh is Cra - zy.";
var words =
Regex
.Matches(test, #"(?<part>\w+)(\s*-\s*(?<part>\w+))+\b")
.Cast<Match>()
.Select(
x => string.Join(
string.Empty,
x.Groups["part"].Captures.Cast<Capture>().SelectMany(capture => capture.Value)))
.ToList();
words is a list containing "vivek","Mahesh","Crazy".
DEMO
Replacing words will work the same way:
var replacingValues = new Dictionary<string, string> { { "Crazy", "XXX" } };
var test = "viv-ek is a good boy.Mah - esh is Cra - zy.";
var replacedTest =
Regex.Replace(
test,
#"\b(?<part>\w+)(\s*-\s*(?<part>\w+))+\b",
match =>
{
var word = string.Join(string.Empty, match.Groups["part"].Captures.Cast<Capture>().SelectMany(capture => capture.Value));
string replacingValue;
return replacingValues.TryGetValue(word, out replacingValue) ? replacingValue : match.Value;
});
replacedTestcontains viv-ek is a good boy.Mah - esh is XXX.
DEMO

How to check a String for characters NOT to be included in C#

So I have a String like:
String myString = "AAAaAAA";
I want to check the String if it contains ANY characters that are not "A"
How can I do this? my previous code is:
Regex myChecker = new Regex("[^A.$]$");
if (checkForIncluded.IsMatch(myString))
{
//Do some Stuff
}
Is there any other way to do it? The code above does not detect the small a. But when I use a different String with only characters that are not "A" it works. Thank you!
String myString = "AAAaAAA";
if(myString.Any(x => x != 'A')) {
// Yep, contains some non-'A' character
}
Try something like this:
var allowedChars = new List<char>() { 'a', 'b', 'c' };
var myString = "abcA";
var result = myString.Any(c => !allowedChars.Contains(c));
if (result) {
// myString contains something not in allowed chars
}
or even like this:
if (myString.Except(allowedChars).Any()) {
// ...
}
allowedChars can be any IEnumerable< char >.
I want to check the String if it contains ANY characters that are not
"A"
You can use Enumerable.Any like;
string myString = "AAAaAAA";
bool b = myString.Any(s => !s.Equals('A')); // True
You can use Linq:
String myString = "AAAaAAA";
var result = myString.Where(x=>x != 'A'); // return all character that are not A
if(result.Count() > 0)
{
Console.WriteLine("Characters exists other than a");
}
if you want both cases:
String myString = "AAAaAAA";
var result = myString.Where(x=>x != 'A' || x != 'a');
or Use String.Equals():
var result = myString.Where(x => !String.Equals(x.ToString(), "A", StringComparison.OrdinalIgnoreCase));
Your regular expression is only trying to match the last character. This should work:
var myString = "AAaA";
bool anyNotAs = Regex.IsMatch(myString, "[^A]", RegexOptions.None);

How do I extract substrings from string?

I have so expression that contains numbers and plus symbols:
string expression = 235+356+345+24+5+2+4355+456+365+356.....+34+5542;
List<string> numbersList = new List<string>();
How should I extract every number substring (235, 356, 345, 24....) from that expression and collect them into a string list?
You can do something like
List<string> parts = expression.Split('+').ToList();
http://msdn.microsoft.com/en-us/library/system.string.split.aspx
If there is any potential for white space around the + signs, you could so something a little more fancy:
List<string> parts = (from t in expression.Split('+') select t.Trim()).ToList();
Something like:
string expression = "235+356+345+24+5+2+4355+456+365+356";
List<string> list = new List<string>(expression.Split('+'));
Try this piece of code
string expression = "235+356+345+24+5+2+4355+456+365+356";
string[] numbers = expression.Split('+');
List<string> numbersList = numbers.ToList();
Or this, a positive check for numeric sequences:
private static Regex rxNumber = new Regex( "\d+" ) ;
public IEnumerable<string> ParseIntegersFromString( string s )
{
Match m = rxNumber.Match(s) ;
for ( m = rxNumber.Match(s) ; m.Success ) ; m = m.NextMatch() )
{
yield return m.Value ;
}
}

Categories

Resources