I have a large file that references a URL within it. It always has the format:
URL="........." - where the ellipses are the URL.
I have a new URL would like to replace the previous with and I was wondering if there was any sort of wildcard string replace.
Example:
A large string contains: URL="google.com"
Problem:
I need to replace the above with: URL="123.com"
Thanks for any help
Try this:
string largeString = ".....URL=\"google.com\" ....";
string pattern = "URL=\"([^ \"]*)\"";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(largeString, "URL=\"123.com\"");
Regex.Replace(line, #"URL\s*=\s*"".+?""", #"URL=""123.com""");
Related
I have such types of file paths:
\\server\folder\folder1\folder3\someFile.txt,
\\otherServer\folder123\folder1\folder3\someFile.txt
\\serv\fold\folder3\folder4\someFile.txt
I need to remove the first two segments of this path to make them as follows:
folder1\folder3\someFile.txt,
folder1\folder3\someFile.txt
folder3\folder4\someFile.txt
I'm doing it with c# and Regex.Replace but need a pattern.
Thanks.
It seems, that you work files' paths, and that's why I suggest using Path not Regex:
using System.IO;
...
string source = #"\\serv\fold\folder3\folder4\someFile.txt";
var result = Path.IsPathRooted(source)
? source.Substring(Path.GetPathRoot(source).Length + 1)
: source;
If you insist on regular expressions then
string result = Regex.Replace(source, #"^\\\\[^\\]+\\[^\\]+\\", "");
You can use the following regular expression pattern to remove the first two segments:
^\\[^\\]+\\[^\\]+\\
you can use Regex.Replace in C#
example:
string input = "\\\\server\\folder\\folder1\\folder3\\filename.txt";
string pattern = "^\\\\[^\\\\]+\\\\[^\\\\]+\\\\";
string replacement = "";
var result = Regex.Replace(input, pattern, replacement);
output:
folder1\folder3\filename.txt
I need to extract from the below string
2_240219_0.vnd as 240219
I have tried as follows: _[0-9]+_
This gives me _240219_
How do I remove the _ from both ends.
I would actually recommend not even using regex in this case. A simple string split on underscore should do just fine:
string input = "2_240219_0.vnd";
string middle = input.Split('_')[1];
Console.WriteLine(middle);
240219
You can try using a other regex: ([\d]{6,})
Match m = Regex.Match(2_240219_0.vnd, `([\d]{6,})`, RegexOptions.IgnoreCase);
I have this string seen here and I want it to become something like this.
I have tried regex with *w460, (w460), w.0 but nothing works
Why you dont use Replace ?
var fullUrl = originalurl.Replace("w460", "full");
With regex:
var regex = new Regex(Regex.Escape("w460"));
var webString = regex.Replace(#"http://media.rtv.net/image/201410/w460/loc_crima_lac_67711100.jpg", "full", 1);
this will replace the first "w460" text
The solution actually depends. What is "w460" a constant or pattern? You can make like below or there could be much more patterns to match the best requieremens
var input = #"http://media.rtv.net/image/201410/w460/loc_crima_lac_67711100.jpg";
var pattern = #"w\d{3}";
var replacement = "full";
var result = Regex.Replace(input, pattern, replacement);
Try this:
var url = "http://media.rtv.net/image/201410/w460/loc_crima_lac_67711100.jpg";
url = url.Replace("/w460/", "/full/");
Use the Replace method of string.
I would't recommend using regex here, it just adds a bit of extra overhead in your code. Creating another instance of an object just to replace a single string occurence? The Replace method of String is ok for this situation, plus it's more readable this way.
For example, my regular expression found the string: some\file\path.xml and I want it to be changed to new_root\some\file\path.xml. Is there a way to do this using the regex replace method? If not, what is the preferred way to do this?
It appears that you can do what you are asking using Regex.Replace.
Check out Substitutions in Regular Expressions article on MSDN.
Example:
var path = #"C:\some\file\path.xml";
var result = Regex.Replace(path, #"(C:\\)(.*)", "$1new_root\\$2");
Result is C:\new_root\some\file\path.xml.
You don't need regex for that, just find the string you want with a buid-in function and concatenate with what you want.
For a more general search/replace you can do this:
string pattern = #"(?>\w+\\)+\w+.xml";
string replacement = "new_root\\$0";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Does anybody know I can replace a group of numbers in a string by one *. For example if I have a string like this "Test123456.txt", I want to convert it to "Test#.txt". I have seen plenty of examples that can replace each individual number with a new character, but none that deal with a group of numbers. Any help is much appreciated!
Regex r = new Regex(#"\d+", RegexOptions.None);
Console.WriteLine(r.Replace("Test123456.txt", "#"));
Console.Read();
Use Regex.Replace() as follows:
string fileName = "Test12345.txt";
string newFileName = Regex.Replace(fileName, #"[\d]+", "#");
you can use regex, to do this, but if you know the exact text, then using the string.Replace method would be more efficient:
string str = "blahblahblahTest123456.txt";
str = string.Replace("Test#.txt","Test123456.txt");