I have a text string, like this [A203][Tom D.][Local.VV-12], now i'm only intrested in the last text, [Local.VV-12], i'm able to delete everything using
string output = Regex.Replace(message, #" ?\[.*?\]", string.Empty);
but that deletes my last one too, how would i go about this?
Change your regex pattern:
string output = Regex.Replace(message, #" ?\[.*?\](?i:\[.*?\])", string.Empty);
Returns [Local.VV-12]
Try this using Substring function
string output=myString.Substring(myString.LastIndexOf("["));
How about this?
string output = Regex.Replace(message, #".*\[", "[");
Related
I have read an entire file into a string object :-
string result = File.ReadAllText(#"C:\TestLog.log");
I want to remove all line breaks, "\n", but i also need to preserve any instances in the string object where "\r\n" exists.
How can i do this?
It looks like you want a regular expression replace.
string result = File.ReadAllText(#"C:\TestLog.log");
string newresult = Regex.Replace(result, #"[^\\r]\\n", "");
So the pattern looks for any \n that is not preceded by \r.
result = result.Replace("\n","").Replace("\r","\r\n")
Without using regex though. Not sure if you intend to use that or not.
How can I trim a string by a whole string instead of a list of single characters?
I want to remove all and whitespaces at beginning and end of an HTML string. But method String.Trim() does only have overloads for set of characters and not for set of strings.
You could use HttpUtility.HtmlDecode(String) and use the resultant as an input for String.Trim()
HttpUtility.HtmlDecode on MSDN
HttpServerUtility.HtmlDecode on MSDN (a wrapper you can access through the Page.Server property)
string stringWithNonBreakingSpaces;
string trimmedString = String.Trim(HttpUtility.HtmlDecode(stringWithNonBreakingSpaces));
Note: This solution would decode all the HTML strings in the input.
The Trim method removes from the current string all leading and trailing white-space characters by default.
Edit: Solution for your problem AFTER your edit:
string input = #" <a href='#'>link</a> ";
Regex regex = new Regex(#"^( |\s)*|( |\s)*$");
string result = regex.Replace(input, String.Empty);
This will remove all trailing and leading spaces and . You can add any string or character group to the expression. If you were to trim all tabs too the regex would simply become:
Regex regex = new Regex(#"^( |\s|\t)*|( |\s|\t)*$");
Not sure if this is what you're looking for?
string str = "hello ";
str.Replace(" ", "");
str.Trim();
Use RegEx, as David Heffernan said. It is rather easy to select all spaces at the start of string: ^(\ | )*
I currently have a regex method that removes a specific series of characters from an existing text file, although how could I do this with a string instead?
e.g. remove any occurrences of "xyz" from s string
The code I have so far:
var result = Regex.Replace(File.ReadAllText(#"Command.bat"), #"test", string.Empty);
System.IO.File.WriteAllText(#"Command.bat", result);
If you want to use string you can use the Replace function of the string.
var line = "bansskgngxyz".Replace("xyz","");
Uhh.. it already does it with a string. ReadAllText returns a string, and WriteAllText writes a string... so all you have to do is change the File.ReadAllText to a string and you're done.
In other words:
var result = Regex.Replace(#"test string", #"test", string.Empty);
System.IO.File.WriteAllText(#"Command.bat", result);
EDIT:
Your code above can be rewritten as:
string s = File.ReadAllText(#"Command.bat");
var result = Regex.Replace(s, #"test", string.Empty);
System.IO.File.WriteAllText(#"Command.bat", result);
So as you can see, Regex.Replace already accepts a string, does this make it any more clear?
string s = "xyz";
var newS = Regex.Replace(s, "test", "");
I have this string
AnyText: "TomDickHarry" <usernameredacted#example.com>
Desired Output Using Regex
AnyText: <usernameredacted#example.com>
Help to replace anything in between AnyText: and <usernameredacted#example.com> with an empty string using Regex.
I am still a rookie at regular expressions. Could anyone out there help me with the matching & replacing expression for the above scenario?
string ABC = "AnyText: \"TomDickHarry\" <usernameredacted#example.com>"
Regex RemoveName = new Regex("(?<=AnyText:).*(?=<)");
string XYZ = RemoveName.Replace(ABC, "");
So, this will find a regex match in the string you supplied, and on the third line, replace it with an empty string.
const string Input = #"AnyText: ""TomDickHarry"" <usernameredacted#example.com>This is.";
var result = Regex.Replace(Input, "(?<=AnyText:)([^<]*)", string.Empty);
This works for me:
string s = Regex.Replace(Input, ":(.*)<", "");
how can I format below string
Address1=+1234+block+of+XYZ+Street+Address2=+Santa+Fe+Springs+State=+California
to string
Address1=+1234+block+of+XYZ+Street+&Address2=+Santa+Fe+Springs+&State=+California
The below regex doesnt work properly.could someone fix this?
string inputString = "Address1=+1234+block+of+XYZ+Street+Address2=+Santa+Fe+Springs+State=+California";
string outString = Regex.Replace(inputString,#"([\s])([a-zA-Z0-9]*)(=)","&$1");
I think you want this
Regex.Replace(inputString,#"\+([a-zA-Z0-9]+)=","+&$1=")
Or this if you want to allow any character other than + & = in keywords.
Regex.Replace(inputString,#"\+([^+&=]+)=","+&$1=")
If all you want to do is prefix "Address2" and "State" by an ampersand:
Regex.Replace(inputString, "(?=Address2=|State=)", "&");