Regex find first 20 characters after an email stored in variable - c#

string subject = null;
string toFind = "["+emailFound+"]([.20])";
Match match = Regex.Match(messageText, toFind, RegexOptions.IgnoreCase);
if (match.Success)
{
subject = match.Value;
}
MessageBox.Show(subject);
This is code I have so far, I'm really new at regex and not quite sure how it works. How would I get the first 20 characters after "emailFound"
Thanks

No more errors, but now it's just not finding anything after the emailFound...
That's because you need to get the Group value after you check the match instance.
string toFind = Regex.Escape(emailFound) + "(?s)(.{20})";
Match match = Regex.Match(messageText, toFind);
if (match.Success) {
subject = match.Groups[1].Value; // Get the Group value
}
See live demo

You can try this:
string toFind = Regex.Escape(emailFound)+"(?s)(.{20})";
(?s) allows the dot to match newlines (you can remove it and add the RegexOptions.SingleLine in the Match method.)
.{20} twenty characters

Related

Regex for this formula

I'm struggling with making a regex for this type of formula: LU1O/00054362/8. I'd like to receive only the first part - LU1O every time. It's always letter, letter, number, letter. Please help me with suggestion.
try this:
string input = #"LU1O/00054362/8";
var regex = "^[A-Z]{2}[0-9][A-Z]$";
var match = Regex.Match(input.Substring(0,4), regex, RegexOptions.IgnoreCase);
if (match.Success)
{
//matched
}
or
string input2 = #"LU1O/00054362/8";
var regex2= "^[A-Z]{2}[0-9][A-Z]";
var match2 = Regex2.Match(input2, regex2, RegexOptions.IgnoreCase);
if (match2.Success)
{
//matched
}
If '/' is always a delimiter you could string.split('/') and access the first element from the resulting array.

Get the Number from a string

I want to trim a string and get the number between special characters.
For example there is a string BC/PO/88/2018 from it i want to get 88.
you can make use of regular expression and extract number
Match match = Regex.Match("BC/PO/88/2018 f" , #"(\d+)");
if (match.Success) {
return int.Parse(match.Groups[0].Value);
}
other way is you can do with the help of String.Split as suggested in comments if you are sure about string coming as input i.e. sure about format of string.
You could use Regular Expressions:
string strRegex = #"[A-Z]{2}/[A-Z]{2}/(?<MyNumber>[0-9]*)/[0-9]{4}";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = #"BC/PO/88/2018";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}

search string for everything before a set of characters in C#

I'm looking for a way to search a string for everything before a set of characters in C#. For Example, if this is my string value:
This is is a test.... 12345
I want build a new string with all of the characters before "12345".
So my new string would equal "This is is a test.... "
Is there a way to do this?
I've found Regex examples where you can focus on one character but not a sequence of characters.
You don't need to use a Regex:
public string GetBitBefore(string text, string end)
{
var index = text.IndexOf(end);
if (index == -1) return text;
return text.Substring(0, index);
}
You can use a lazy quantifier to match anything, followed by a lookahead:
var match = Regex.Match("This is is a test.... 12345", #".*?(?=\d{5})");
where:
.*? lazily matches everything (up to the lookahead)
(?=…) is a positive lookahead: the pattern must be matched, but is not included in the result
\d{5} matches exactly five digits. I'm assuming this is your lookahead; you can replace it
You can do so with help of regex lookahead.
.*(?=12345)
Example:
var data = "This is is a test.... 12345";
var rxStr = ".*(?=12345)";
var rx = new System.Text.RegularExpressions.Regex (rxStr,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
var match = rx.Match(data);
if (match.Success) {
Console.WriteLine (match.Value);
}
Above code snippet will print every thing upto 12345:
This is is a test....
For more detail about see regex positive lookahead
This should get you started:
var reg = new Regex("^(.+)12345$");
var match = reg.Match("This is is a test.... 12345");
var group = match.Groups[1]; // This is is a test....
Of course you'd want to do some additional validation, but this is the basic idea.
^ means start of string
$ means end of string
The asterisk tells the engine to attempt to match the preceding token zero or more times. The plus tells the engine to attempt to match the preceding token once or more
{min,max} indicate the minimum/maximum number of matches.
\d matches a single character that is a digit, \w matches a "word character" (alphanumeric characters plus underscore), and \s matches a whitespace character (includes tabs and line breaks).
[^a] means not so exclude a
The dot matches a single character, except line break characters
In your case there many way to accomplish the task.
Eg excluding digit: ^[^\d]*
If you know the set of characters and they are not only digit, don't use regex but IndexOf(). If you know the separator between first and second part as "..." you can use Split()
Take a look at this snippet:
class Program
{
static void Main(string[] args)
{
string input = "This is is a test.... 12345";
// Here we call Regex.Match.
MatchCollection matches = Regex.Matches(input, #"(?<MySentence>(\w+\s*)*)(?<MyNumberPart>\d*)");
foreach (Match item in matches)
{
Console.WriteLine(item.Groups["MySentence"]);
Console.WriteLine("******");
Console.WriteLine(item.Groups["MyNumberPart"]);
}
Console.ReadKey();
}
}
You could just split, not as optimal as the indexOf solution
string value = "oiasjdoiasj12345";
string end = "12345";
string result = value.Split(new string[] { end }, StringSplitOptions.None)[0] //Take first part of the result, not the quickest but fairly simple

Regex for a special string pattern on multiple levels

I need to evaluate a string pattern that is something like as given below and I am quite new at writing such complex expressions
<%(ddlValue){DropDownList}[SelectedValue]%>
// this has three part (Control Name) {Control Type} [Control Property]
I tried a whole lot of regex and other tools like RegExr but anything did not worked. I have to do this on four levels, that is as given below in the code. So here is what I have done:
string regex = "/[<%](.*?)[%>]/g"; // Regex to match "<% %>" pattern
Match mtch = Regex.Match(strQuery, regex, RegexOptions.Singleline);
string strControlName = "";
string strControlType = "";
string strControlProp = "";
if (mtch.Success)
{
string strVal = mtch.Value;
Match mtchControlName = Regex.Match(strVal, "/[(]\S)/");
// Regex to match "()" i.e. control name ("ddlValue" in above example)
if (mtchControlName.Success)//Match control Name
{
strControlName = mtchControlName.Value;
Match mtchControlType = Regex.Match(strVal, "/[{]\s[}]/");
// Regex to match "[]" i.e. control type
if (mtchControlType.Success) // Match Control Type
{
strControlType = mtchControlType.Value;
Match mtchControlProp = Regex.Match(strVal, "/[(]\S[)]/");
// Regex to match "[]" i.e. control property
if (mtchControlProp.Success) // Match Control Prop
{
strControlProp = mtchControlProp.Value;
}
}
}
}
You can do this in a single regex. Being as specific as possible, you could do this:
Regex regexObj = new Regex(
#"\( # Match (
( # Capture in group 1:
[^()]* # Any number of characters except ()s
) # End of group 1
\) # Match )
\{([^{}]*)\} # The same for {...} into group 2
\[([^\[\]]*)\] # The same for [...] into group 3",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
Then you case use
Match matchResults = regexObj.Match(subjectString);
to get a Match object. Access the submatches via
matchResults.Groups(n).Value // insert 1, 2 or 3 for n
See it live on regex101.com.
You can use capturing groups in one expression to capture all groups together:
String input = "<%(ddlValue){DropDownList}[SelectedValue]%>";
String pattern = #"<%\((.+)\)\{(.+)\}\[(.+)\]%>";
Match m = Regex.Match(input, pattern);
if (m.Groups.Count == 4)
{
string firstpart = m.Groups[1].ToString();
string secondpart = m.Groups[2].ToString();
string thirdpart = m.Groups[3].ToString();
}
You could use named groups. It makes the code more readable.
Match m = Regex.Match(inputData, #"^<%\((?<ddlValue>[^)]+)\){(?<DropDownList>[^}]+)}\[(?<SelectedValue>[^\]]+)\]%>$");
if (m.Groups.Count == 4)
{
string firstpart = m.Groups["ddlValue"].ToString();
string secondpart = m.Groups["DropDownList"].ToString();
string thirdpart = m.Groups["SelectedValue"].ToString();
}

C# Regular Expressions, string between single quotes

string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
i want to get the text between ' quotes using Regular Expressions.
Can anyone?
Something like this should do it:
string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
Match match = Regex.Match(val, #"'([^']*)");
if (match.Success)
{
string yourValue = match.Groups[1].Value;
Console.WriteLine(yourValue);
}
Explanation of the expression '([^']*):
' -> find a single quotation mark
( -> start a matching group
[^'] -> match any character that is not a single quotation mark
* -> ...zero or more times
) -> end the matching group
You are looking to match GUID's in a string using a regular expression.
This is what you want, I suspect!
public static Regex regex = new Regex(
"(\\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-"+
"([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\\}{0,1})",RegexOptions.CultureInvariant|RegexOptions.Compiled);
Match m = regex.Match(lineData);
if (m.Succes)
{
...
}
This will extract the text between the first and last single quote on a line:
string input = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
Regex regName = new Regex("'(.*)'");
Match match = regName.Match(input);
if (match.Success)
{
string result = match.Groups[1].Value;
//do something with the result
}
You could use positive lookahead and lookbehind also,
string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
Match match = Regex.Match(val, #"(?<=')[^']*(?=')");
if (match.Success)
{
string yourValue = match.Groups[0].Value;
Console.WriteLine(yourValue);
}

Categories