Regex pattern for any string in between - c#

I want to replace strings like
Dependencies\myfolder\1\2\abc.dll
Dependencies\myfolder\abc.dll
Dependencies\myfolder\1\abc.dll
with
packages\abc.dll.
What is the suitable regex pattern to do this. I was expecting the pattern to be -
Dependencies*abc.dll
So my code is -
var newEntry = packages\abc.dll;
var pattern = Dependencies*abc.dll;
var allText = ""; //this contains the text read from a file
Regex rgx = new Regex(pattern);
rgx.Replace(allText, newEntry);
But this seems to be a wrong regex pattern.

Almost there you need the .* like:
Dependencies(.*)abc.dll
Online Demo
.NET Online Demo

Related

C# Regex finds only one match

I have a regex pattern which extract url and link text to turns custom tag to tag.
When i try my pattern on online checker it find 4 matceh, but when i run my code c# finds only one match.
Regex rgx = new Regex(#"(\[)+(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)( )(.)+(\])");
The Entry is like
[http://facebook.com/ LinkText]
[http://youtube.com/ LinkText]
[http://instagram.com/ LinkText]
[https://stackoverflow.com/users/1131979/cagri-d-kaynar LinkText]
My Code
Regex rgx =
new Regex(#"(\[)+(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)( )(.)+(\])");
foreach (Match match in rgx.Matches(entry))
{
var matchv = match.Value;
/*extract url and Link text from match value*/
var bknz =
String.Format("{1}", cc[0], cc[1]);
entry = rgx.Replace(entry, bknz);
}
Whats wron with my code? Did i missa flag or ste?
I replace the \r\n to <br /> before Regex pattern match and it causes the getting one match.
I do the replacement after checking regex matches. Now it's working well

C# Regex is matching too much

My regex should work like this: https://regex101.com/r/dY9jI4/1
It should Match only Nicknames (personaname).
My C# code looks like this:
string pattern = #"personaname"":\s+""([^""]+)""";
string users = webClient.DownloadString("http://pastebin.com/raw/cDHTXXD3");
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(users);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
But in VS15 my regex in matching my whole pattern, so console output looks like:
personaname": "Tom"
personaname": "Emily"
Is something wrong with my pattern? How can I fix it?
So while the actual answer would be to just parse this JSON and get the data, your problem is in Match, you need match.Groups[1].Value to get that unnamed group.
string pattern = #"personaname"":\s+""(?<name>[^""]+)""";
string users = webClient.DownloadString("http://pastebin.com/raw/cDHTXXD3");
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(users);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups["name"].Value);
}

Replace part of RegEx pattern in C#

The code below works
string pattern = "\"cs\":\".*?\"";
string replacement = "\"cs\":\"\"";
Regex rgx = new Regex(pattern);
string stripped = rgx.Replace(resp.JsonResponse, replacement);
But is it possible to tweak this so that I only replace .*? with nothing.
string pattern = "(exclude:\"cs\":\").*?(exclude:\")";
string replacement = "";
Regex rgx = new Regex(pattern);
string stripped = rgx.Replace(resp.JsonResponse, replacement);
Yes, by only matching .*? and adding positive lookahead/lookbehind assertions to ensure it only matches when the conditions are correct.
So, change your pattern to
(?<="cs":").*?(?=")
(unescaped)
Now, you'll only match .*? when it is preceded by "cs":" and followed by ".
Q: It looks like you're editing JSON. Is there a reason that you don't want to defer this work to a JSON parser?

Regular expression in C# to find matches like "x-[some_string]"

What is the Regular Expression in C# to find matches inside text
that starting with "x-[" and ending with "]"?
I've tried something like this:
Regex urlRx = new Regex(#"^x-[.*]$", RegexOptions.IgnoreCase);
Simple:
x-\[([^]]+)\]
# that is: look for x-[ literally
# capture and save anything that is not a ]
# followed by ]
See a demo on regex101.com.
This should work
string input = "x-[ABCD]";
string pattern = "^x-\\[(.*)\\]$";
Regex rgx = new Regex(pattern);
Match match = rgx.Match(input);
if (match.Success) {
Console.WriteLine(match.Groups[1].Value);
}
IDEONE DEMO
UPDATE
As pointed by Jan, there will be too much backtracking in cases like x-[ABCDEFGHJJHGHGFGHGFVFGHGFGHGFGHGGHGGHGDCNJK]ABCD]. My updated regex is similar to his
^x-\[([^\]]*)\]$
Do you really need a regex for it? Simple String operation should serve your purpose.
yourString.EndsWith("]");
yourString.StartsWith("x-[");

value extraction using regular expressions

I have a text file that contains commands in this format ex:>> call <<
I want to use a regular expression to extract "call" ..
how could be done?
Regex ComandStart = new Regex(">>", RegexOptions.Multiline);
Regex ComandEnd = new Regex("<<", RegexOptions.Multiline);
create the regex:
var regex = new Regex(#"ex:>>([a-z]+)<<");
then extract if match :
var match = regex.Match("ex:>>call<<");
var yourString = match.Groups[1].Value; //yourString = "call" here
If you're on Windows, you could use Notepad++ with the Regex helper plugin for testing your regex's
This expression works fine for me:
/ex:\>\>(.*)\<\</
No idea about c# specifically, sorry

Categories