I've been trying to capture the last folder in a folder path using regular expressions in C# but am just too new to this to figure this out. For example if I have C:\Projects\Test then the expression should return Test. If I have H:\Programs\Somefolder\Someotherfolder\Final then the result should be Final. I've tried the below code but it just blows up. Thanks for any help.
string pattern = ".*\\([^\\]+$)";
Match match = Regex.Match("H:\\Projects\\Final", pattern, RegexOptions.IgnoreCase);
Why are you using a regex. You can just use DirectoryInfo.Name
var directoryname = new DirectoryInfo(#"C:\Projects\Test").Name;
\\The variable directoryname will be Test
this is a bad use of regular expressions when you have a pretty complete set of .NET libraries that can do this for you... two easy methods using System.IO.Path or System.IO.DirectoryInfo below
string path = #"H:\Programs\Somefolder\Someotherfolder\Final";
Console.WriteLine(System.IO.Path.GetFileName(path));
Console.WriteLine(new System.IO.DirectoryInfo(path).Name);
Perhaps this?
string strRegex = #".*\\(.*)"; RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"H:\Programs\Somefolder\Someotherfolder\Final";
string strReplace = #"$1";
return myRegex.Replace(strTargetString, strReplace);
Why don't use split?
string str = "c:\temp\temp1\temp2" ;
string lastfolder = str.Split("\").Last ;
Related
Im have a program that vbcan only handle basic regex no C# vb.net etc.
This is my situation.
I have a set of start Urls.
http://www.foo.com?code=234654
I need to remove the ?code= and replace with a / then add the letter t at the end.
Like this:
http://www.foo.com/234654t
I would appreciate any help this this.
Thanks
Sean
For the dialect that is used in java.util.regex you can use this regular expression, for example:
String regex = "\\?+[A-Za-z=]+([0-9]+)(?<=[0-9]+)(?=$)";
String replacement = "/$1t";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(line);
if (m.find()) {
System.out.println(m.replaceAll(replacement));
}
Another example, by using replaceAll:
line.replaceAll("\\?+[A-Za-z=]+", "/").replaceAll("(?<=[0-9|/]+)(?=$)", "t");
For the string:
String line = "http://www.foo.com?code=234654";
You'll get:
http://www.foo.com/234654t
Please help me. I have a strange error with Regex:
Const AGC = "s$(.*)s"
Dim ORIGINAL As String = "s$1s"
Dim lel As Regex = New Regex(AGC)
Dim lol As Match = lel.Match(ORIGINAL)
MsgBox(lol.Success)
MsgBox(lol.Groups(0).Value)
The following code doesn't work , i don't know why.
This is because the $ is a special character. You need to escape it in your Regex:
Const AGC = "s\$(.*)s"
The rest of the code should work fine:
Const AGC = "s\$(.*)s"
Dim ORIGINAL As String = "s$1s"
Dim lel As Regex = New Regex(AGC)
Dim lol As Match = lel.Match(ORIGINAL)
MsgBox(lol.Success)
MsgBox(lol.Groups(0).Value)
See it in action here.
I posted a C# version of this answer in the previous revision accidentally!
$ is a special regex character showing end-of-line. You'll have to use \$ to actually specify the dollar character in the regex expression. Your new expression will be "s\$(.*)s". Use sites such as Regex Storm .Net tester to test your regexes when you're new to them. Link to this regex and its test:
http://regexstorm.net/tester?p=s%5c%24(.*)s&i=s%241s
I have a list of strings looking like this:
Title_in_Title_by_-_Mr._John_Doe
and I need to replace the _ with a SPACE from the text between the html"> and </a> ONLY.
so that the result to look like this:
Title in Title by - Mr. John Doe
I've tried to do it in 2 steps:
first isolate that part only with .*html">(.*)<\/a.* & ^.*>(.*)<.* & .*>.*<.* or ^.*>.*<.*
and then do the replace but the return is always unchanged and now I'm stuck.
Any help to accomplish this is much appreciated
How I would do it is to .split it and then .replace it, no need for regex.
Dim line as string = "Title_in_Title_by_-_Mr._John_Doe"
Dim split as string() = line.split(">"c)
Dim correctString as String = split(1).replace("_"c," "c)
Boom done
here is the string.replace article
Though if you had to use regex, this would probably be a better way of doing it
Dim inputString = "Title_in_Title_by_-_Mr._John_Doe"
Dim reg As New Regex("(?<=\>).*?(?=\<)")
Dim correctString = reg.match(inputString).value.replace("_"c, " "c)
Dim line as string = "Title_and_Title_by_-_Mr._John_Doe"
line = Regex.Replace(line, "(?<=\.html"">)[^<>]+(?=</a>)", _
Function (m) m.Value.Replace("_", " "))
This uses a regex with lookarounds to isolate the title, and a MatchEvaluator delegate in the form of a lambda expression to replace the underscores in the title, then it plugs the result back into the string.
I am searching for a Regular Expression that can help me extract filename.asp from the below string. It seems like a simple task, but I am unable to find a solution.
This is my input:
<!-- #include file="filename.asp" -->
I want output of regular expression like this:
filename.asp
I did some research and find the following solution.
Regular Expression:
/#include\W+file="([^"]+)"/g
Example code (VB.NET):
Dim list As New List(Of String)
Dim regex = New System.Text.RegularExpressions.Regex("#include\W+file=""([^""]+)""")
Dim matchResult = regex.Match(filetext)
While matchResult.Success
list.Add(matchResult.Groups(1).Value)
matchResult = matchResult.NextMatch()
End While
Example code (C#):
var list = new List<string>();
var regex = new Regex("#include\\W+file=\"([^\"]+)\"");
var matchResult = regex.Match(fileContent);
while (matchResult.Success) {
list.Add(matchResult.Groups[1].Value);
matchResult = matchResult.NextMatch();
}
Improved Regular Expression (ignores spaces):
#include\W+file[\s]*=[\s]*"([^"]+)"
Very soring about posting this question, but even if i am a noob in RegEx, i'v started to learn them by myself, but i didn't get the answer for this case.
I am using group, and i wanted to exclude from my result the search caractere i 've used. This is an exemple :
My String : Hand #97407583861: dhihidhi Hand #97407583862: djodjidji Hand #97407583863:
The Regex i use : (?Hand.#\d*:)
The results, i wanted to have in my groups :
97407583861
97407583862
97407583863
97407583864
Best regards
If I understood you correctly, this should help:
string pattern = #"Hand\s#(?<Num>\d+):";
RegexOptions regexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex regex = new Regex(pattern, regexOptions);
string targetString = #"Hand #97407583861: dhihidhi Hand #97407583862: djodjidji Hand #97407583863:";
foreach (Match match in regex.Matches(targetString))
{
if (match.Success)
{
var number = match.Groups["Num"].Value;
}
}
Tested using RegexHero.