This question already has answers here:
How to read RegEx Captures in C#
(3 answers)
Closed 7 years ago.
First of all sorry about the tittle. Didn't know how to explain it.
This is my code:
string sString = #"docs/horaires/1/images/1"
var PickImage = Regex.Matches(sString, "/horaires/(.*?)/images/");
Console.WriteLine(PickImage[0].Value);
This will print /horaires/1/images/ instead of 1. I tried all the RegexOptions but didn't find the solution in there.
What am I doing wrong here?
This is what you need:
string sString = #"docs/horaires/1/images/1";
var pickImage = Regex.Match(sString, #"/horaires/(.*?)/images/");
if (pickImage.Success)
Console.WriteLine(pickImage.Groups[1].Value);
In your original code, PickImage[0] is a Match object, and Value will return the full match. You want the first captured group, so use match.Groups[1].Value. Note that Groups[0] always contains the full match.
No need to use Matches is you want a single result, use Match instead.
Related
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
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.
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.
This question already has answers here:
C# string replacement , not working
(6 answers)
Closed 8 years ago.
I have a string like
mukesh "salaria" engineer
how do i replace " with blank
like i want output as
mukesh salaria engineer
I already tried, str.replace("\"",string.empty);
but it doesn't working for me.
It works but must type this: str = str.replace(...)
string.Replace is a pure method(no side-effects). If you do not assign str.replace("\"",string.empty); to anything, then that statement does not make any change in the state of your object(in other words writing that line of code is equal to not writing it at all.).
Use str = str.Replace(...);
You must use the Replace method and get the string back from it. Your code must be:
str = str.Replace("\"", string.Empty);
msdn :
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
try this
str.Replace('\"',' ');
This question already has answers here:
How to identify if a string contains more than one instance of a specific character?
(4 answers)
Closed 8 years ago.
I think my question is simple.
I just need a little character check when I click in one button. If such textbox have more than one comma, I want a message: "Error. Please insert just one comma in the box."
I was searching but have not found anything like this. Can someone help me? Thanks all in advance!
According to this POST:
You can compare IndexOf to LastIndexOf to check if there is more than one specific character in a string without explicit counting:
var s = "12121.23.2";
var ch = '.';
if (s.IndexOf(ch) != s.LastIndexOf(ch)) {
...
}
In your case:
var s = "Comma, another comma, something.";
var ch = ',';
if (s.IndexOf(ch) != s.LastIndexOf(ch)) {
...
}
You can set a boolean when you check if there is a another comma in the string (Textbox.Text).
As listed above here is another POST concerning your question.
As stated in the comments, you can use .Count(). Look HERE for more.