How to match 2 substrings in responce with regex in c# [duplicate] - c#

This question already has answers here:
Returning only part of match from Regular Expression
(4 answers)
Closed 4 years ago.
I have a responce string
"c=2020&action=approvecomment&_wpnonce=7508ac918a' data-wp-lists='dim:the-comment-list:comment-2020:unapproved:e7e7d3:e7e7d3:new=approved"
Im trying to extract 2020 and 7508ac918a. I dont understand how I must use regex with substrings in C#, simple regex like
c=(\d+)&action=approvecomment&_wpnonce=(.*?)' .+new=approved.

In Regex, you can create match groups
They look like this (?.+?)
So your _wpconce part could become something like this (?.*?)
Then you can grab each group individually for example
Match result = myRegex.Match(someString);
soneOtherString = result.Groups["GROUPNAME"].Value;
I use Regex101 to build and test my regex. (Whoever made that site deserves a crown with shinny stones on it!! :)
https://regex101.com/
Hope this helps

Related

how can i extract a specific part of a string in c# with regex.match? [duplicate]

This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 3 years ago.
Though this is probably a duplicate, i haven't been able to make this work after reading similar questions. Looking for help please with this homework, i'm a newbie at programming.
I'm working on a program in C#. I have a text that contains for example this sentence: "My homework version V0.90 from". I need to extract "V0.90", and that may vary from anything between V0.90 to V2.00. It is always surrounded by "My homework version " and " from", i need to extract whatever is between that. This is what i've tried:
string RetRegMatch;
Match Match1 = Regex.Match(Friends[a], #"My homework version (.+) from", RegexOptions.IgnoreCase);
RetRegMatch = Match1.Value;
But i'm geeting as a result in Match1.Value this: "My homework version V0.90 from", and i only want the (+.) part to be in Match1.Value. That is, i would like to have in Match1.Value this: "V0.90". How can i do this?
The part you need should be in the capture group (the part you put between ()). Try accessing it with
Match1.Groups[1].Value

C# Regex - match a substring in a filename [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Given a filesystem path, is there a shorter way to extract the filename without its extension?
(10 answers)
Closed 4 years ago.
My regex is really poor so I need help with a c# regex expression that can match a substring after the last backslash.
Typical input:
D:\DataFiles\Files_81\aars2016FAKH1800010.pdf
I need to check if the filename aars2016FAKH1A800010.pdf contains "FAKH1". It is important that only the filename is evaluated.
It must be done with C# regex, so please no "Contains"
You might be wondering why regex, but this is going to be used in a generic c# application that can evaluate regex expressions.
Thank you in advance.
You can try to use \\\w*(FAKH)\w*\.pdf pattern.
bool isExsit = Regex.IsMatch(#"D:\DataFiles\Files_81\aars2016FAKH1800010.pdf", #"\\\w*(FAKH)\w*\.pdf");
EDIT
You can use Groups[1].Value get FAKH
var result = Regex.Match(#"D:\DataFiles\Files_81\aars2016FAKH1800010.pdf", #"\\\w*(FAKH)\w*\.pdf");
var FAKH = result.Groups[1].Value;
c# online

Regex Pattern to check only letters [duplicate]

This question already has answers here:
C# Regex to allow only alpha numeric
(6 answers)
Closed 5 years ago.
I need to check whether string is only contain letters but not numbers or special characters.I used below regex pattern,
String validText = "^[a-zA-Z-]+$";
its work fine for 'Leo#' but if it is like 'Leo#1' its not working properly.
Anyone have idea ?
I prefer you can use LinQ (input is your test string)
bool result = input.All(Char.IsLetter);
Else as Gordon Posted the right Regex,
^[a-zA-z]+$
You can try using this regex
/^[A-Za-z]+$/
This will match only letters in your string ..

What regex expression would be appropriate? [duplicate]

This question already has answers here:
How to use string.Endswith to test for multiple endings?
(9 answers)
Closed 5 years ago.
I need to check if a string last word is either "...abc" or "...xyz" or "...fgh".
How i can achieve the same thing using regex as i am trying to learn it?
e.g Sentence 1: Hi My Name is abc.
Sentence 2: I live in xyz.
The above sentence is a sample one to demonstrate.
You don't need any Regex. Just use String.EndsWith :
string a = "asdasd abc";
Console.WriteLine(a.EndsWith("abc.") || a.EndsWith("xyz.") || a.EndsWith("fgh."));
You can use this simple regex pattern:
(abc|xyz|fgh)$
Put your possible options between parenthesis separated by pipes. The $ means the end of the string.

Need to get so substring but using Regular Expression [duplicate]

This question already has answers here:
C# Substring Alternative - Return rest of line within string after character
(5 answers)
Closed 6 years ago.
I got on url like.
http://EddyFox.com/x/xynua
Need to fetch substring after /x/ what ever string is there.
complex example I faced is :
http://EddyFox.com/x//x/
Here result should be /x/
It can be achieved with substring ,But we need to perform it with regular expression.
This should do it:
string s = "http://EddyFox.com/x/xynua";
// I guess you don't want the /x/ in your match ?=!
Console.WriteLine(Regex.Match(s, "/x/(.*)").Groups[1].Value );
this is probably even better:
Console.WriteLine(Regex.Match(s, "(?<=/x/)(.*)").Value );
the output is
xynua
Have a look at this post: Regex to match after specific characters SO is full of RegEx posts. The probability is very high that a RegEx question has already been asked before. :)
The regex /x/(.*) will capture everything following the /x/
And where is the problem?
var r = new Regex("/x/(\\S*)");
var matches = r.Matches(myUrl);
This regex matches everything from /x/ until the first occurence of a white-space.

Categories

Resources