C# Regex search string for text including surrounding brackets - c#

I would like to search a string for '[E1010]' or '[E1011]' or '[E1012]'. Currently, I can only successfully search without using the brackets []. How can I adjust my regex to include the texting surrounded by the brackets as it is in my sClientError variable.
Thanks!
string sClientErrors = "Bla Blah \"30\" [E1011]\r\nBlah Blah"44\" [E1012]";
Regex myRegexE10 = new Regex(#"\bE1010\b");
Regex myRegexE11 = new Regex(#"\bE1011\b");
Regex myRegexE12 = new Regex(#"\bE1012\b");
if (myRegexE10.IsMatch(sClientErrors) || myRegexE11.IsMatch(sClientErrors) || myRegexE12.IsMatch(sClientErrors))
{
// do code here...
}

By adding the brackets:
Regex myRegexE10 = new Regex(#"\[E1010]");
or
Regex myRegexE1x = new Regex(#"\[E101[012]]");
if (myRegexE1x.IsMatch(sClientErrors)) { ...
Note that once you add the brackets, word boundaries are no longer necessary. Note too that you don't need to escape closing square brackets

You can put a "\" if front of a character you want to include, so you would use:
Regex myRegexE10 = new Regex(#"\[\bE1010\b\]")
You can also use "\\" if you needed to find something like "\s", where "\*" is a Regex option.

Related

How remove escape characters

In the page script I have:
imgLocStr[0][0] = "gau"
imgLocStr[1][0] = "swo"
imgLocStr[2][0] = "gau"
imgLocStr[0][1] = "swo"
imgLocStr[1][1] = "swo"
imgLocStr[2][1] = "swo"
imgLocStr[0][2] = "swo"
imgLocStr[1][2] = "swo"
imgLocStr[2][2] = "swo"
But when Im parsing the html code I got"
"imgLocStr[0][0] = \"gau\"\n "
How I remove the \ and and \n at the end only need the values "gau" or "swo"
I can use IndexOf("\") and SubString() but Im sure there is an easy way to do this.
You can use Regex.Replace(originalString, #patern, string_to_sustitute)which returns a new string with all the apperances of the pattern replaced with the string_to_sustitute
Try Regex: "\w+" to get "gau" or "sau".
Demo
use Regex.Match in C#
The backslashes \" are to escape the double quote in the string.
To remove the newline and the space at the end you might use \s+$ to match 1+ times a whitespace character till the end of the string.
Fo example:
String str = "imgLocStr[0][0] = \"gau\"\n ";
Console.WriteLine(Regex.Replace(str, #"\s+$", "")); // imgLocStr[0][0] = "gau"
To get your value using a regex you might use an alternation and specify your allowed values:
"(?:gau|swo)"\s*$
demo
Or use a negated character class to match not a double quote or a newline:
"[^"\r\n]+"\s*$
demo
You could specify a more exact pattern using an anchor to assert the string of the string ^ and specify what you would allow between the opening and the closing quotes at the end of the string $:
^imgLocStr\[\d+\]\[\d+\] = ("\w+")\s*$
demo

regex replace matchEvaluator using string Array

I need to highlight search terms in a block of text.
My initial thought was looping though the search terms. But is there an easier way?
Here is what I'm thinking using a loop...
public string HighlightText(string inputText)
{
string[] sessionPhrases = (string[])Session["KeywordPhrase"];
string description = inputText;
foreach (string field in sessionPhrases)
{
Regex expression = new Regex(field, RegexOptions.IgnoreCase);
description = expression.Replace(description,
new MatchEvaluator(ReplaceKeywords));
}
return description;
}
public string ReplaceKeywords(Match m)
{
return "<span style='color:red;'>" + m.Value + "</span>";
}
You could replace the loop with something like:
string[] phrases = ...
var re = String.Join("|", phrases.Select(s => Regex.Escape(s)).ToArray());
text = Regex.Replace(re, text, new MatchEvaluator(SomeFunction), RegexOptions.IgnoreCase);
Extending on Qtax's answer:
phrases = ...
// Use Regex.Escape to prevent ., (, * and other special characters to break the search
string re = String.Join("|", phrases.Select(s => Regex.Escape(s)).ToArray());
// Use \b (expression) \b to ensure you're only matching whole words, not partial words
re = #"\b(?:" +re + #")\b"
// use a simple replacement pattern instead of a MatchEvaluator
string replacement = "<span style='color:red;'>$0</span>";
text = Regex.Replace(re, text, replacement, RegexOptions.IgnoreCase);
Not that if you're already replacing data inside HTML, it might not be a good idea to use Regex to replace just anything in the content, you might end up getting:
<<span style='color:red;'>script</span>>
if someone is searching for the term script.
To prevent that from happening, you could use the HTML Agility Pack in combination with Regex.
You might also want to check out this post which deals with a very similar issue.

regex and string

Consider the following:
string keywords = "(load|save|close)";
Regex x = new Regex(#"\b"+keywords+"\b");
I get no matches. However, if I do this:
Regex x = new Regex(#"\b(load|save|close)\b");
I get matches. How come the former doesn't work, and how can I fix this? Basically, I want the keywords to be configurable so I placed them in a string.
The last \b in the first code snippet needs a verbatim string specifier (#) in front of it as well as it is a seperate string instance.
string keywords = "(load|save|close)";
Regex x = new Regex(#"\b"+keywords+#"\b");
You're missing another verbatim string specifier (# prefixed to the last \b):
Regex x = new Regex(#"\b" + keywords + #"\b");
Regex x = new Regex(#"\b"+keywords+#"\b");
You forgot additional # before second "\b"

How to match an enclosing substring using regex in .NET

I'm trying to match * in id=resultsStats>*<nobr> to extract the middle bit.
This would match e.g.
id=resultsStats>3<nobr>
id=resultsStats>anything<nobr>
so I can extract the middle "3" or "anything"
How do I do this in .NET regex or otherwise?
(?<=id=resultsStats>).+?(?=<nobr>)
Use * instead of + if content is optional rather than required.
Example of use (F#):
open System.Text.RegularExpressions
let tryFindResultsStats input =
let m = Regex.Match (input,
"(?<=id=resultsStats>).+?(?=<nobr>)",
RegexOptions.Singleline)
if m.Success then Some m.Value else None
I'm not a regex expert but something like this might work:
#"\>\*{1}\<"
This means "match a single asterisk between the lt/gt characters". You just need to make sure you escape the asterisk because it has special meaning in regular expressions.
Hope this helps!
If you are looking to capture a * then you need to escape it with a backslash. Note that if you are doing this within a string it is safest to escape the backslash as well (Although technically \* isn't valid and will work)
"\\*"
Try this:
using System;
using System.Text.RegularExpressions;
namespace SO6312611
{
class Program
{
static void Main()
{
string input = "id=resultsStats>anything<nobr>";
Regex r = new Regex("id=resultsStats>(?<data>[^<]*)<nobr>");
Match m = r.Match(input);
Console.WriteLine("Matched: >{0}<", m.Groups["data"]);
}
}
}

string manipulation in regex

i have a problem in string manipulation
here is the code
string str = "LDAP://company.com/OU=MyOU1 Control,DC=MyCompany,DC=com";
Regex regex = new Regex("OU=\\w+");
var result = regex.Matches(str);
var strList = new List<string>();
foreach (var item in result)
{
strList.Add(item.ToString().Remove(0,3));
}
Console.WriteLine(string.Join("/",strList));
the result i am getting is "MyOU1" instead of getting "MyOU1 Control"
please help thanks
If you want the space character to be matched as well, you need to include it in your regex. \w only matches word charactes, which does not include spaces.
Regex regex = new Regex(#"OU=[\w\s]+");
This matches word characters (\w) and whitespace characters (\s).
(The # in front of the string is just for convenience: If you use it, you don't need to escape backslashes.)
Either add space to the allowed list (\w doesn't allow space) or use the knowledge that comma can be used as a separator.
Regex regex = new Regex("OU=(\\w|\\s)+");
OR
Regex regex = new Regex("OU=[^,]+");

Categories

Resources