This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I'm looking for a way to find and extract the string matching a pattern in a string without a space :
string regexpattern = #"[A-Z]{4}\d{4}$"; // ex : BERF4787
string stringWithoutSpace = "stringsampleBERF4787withpattern";
string stringMatchPattern = ??? //I want to get BEFR4787 in this variable
You are almost there. The problem in your pattern is the $ which matches the end of a string. Since in your example the "BERF4787" is located in the middle of the string you should simply remove it:
string regexpattern = #"[A-Z]{4}\d{4}"; // ex : BERF4787
string stringWithoutSpace = "stringsampleBERF4787withpattern";
If you want to match your pattern in a string you can use the Regex.Match method which returns an object of type Match.
To get the matched value you need to use the Match.Value property like this:
string stringMatchPattern = Regex.Match(stringWithoutSpace, regexpattern).Value;
Related
This question already has answers here:
How do I read and parse an XML file in C#?
(12 answers)
Closed 2 years ago.
I have an xml file and I wanted to search the file using the regular expression.
My used regular expression:
(? <= <Name> Description <\ / Name> <Value>). *? (? = <\ / Value>)
And replace the expressions found by a left(expression, 15). There are situations where the string is too long and you need to truncate to left 15.
Example:
https://regex101.com/r/Etfpol/3
The text found is:
Solution changed from
Resolved Time changed
Updated By changed from
I want to replace:
Solution change
Resolved Time c
Updated By chan
My tried Code:
System.IO.StreamReader file = new System.IO.StreamReader(Dts.Connections["xmlfile.xml"].ConnectionString);
Dts.Variables["varXML"].Value = file.ReadLine();
String teste = Dts.Variables["varXML"].Value.ToString();
string pattern = #"(?<=<Name>Description<\/Name><Value>).*?(?=<\/Value>)";
string result = Regex.Replace(teste, pattern, ); Dts.Variables["varXML"].Value = result;
Thanks.
first, for reference, your original Regex from your example was
(?<=<Name>Description<\/Name><Value>).*?(?=<\/Value>)
I modified it to this:
(?<=<Name>Description<\/Name><Value>)(?<Text>.{0,15}).*?(?=<\/Value>)
It captures now the first up to 15 characters in the named group 'Text' and discards the remaining characters, if there are any.
Now you can simply output just the named group:
${Text}
Here is your modified example: https://regex101.com/r/Etfpol/4
Try this to get a replacement result:
string result = Regex.Replace(teste, pattern, delegate(Match match)
{
string v = match.ToString();
return v.Substring(0, 15);
});
Source: system.text.regularexpressions.regex.replace
I use matchevaluator Delegate for this.
This question already has answers here:
regex 'literal' meaning in regex
(1 answer)
How to make a regex match case insensitive?
(1 answer)
Closed 4 years ago.
I need to build regex dynamically, so I pass to my method a string of valid characters. Then I use that string to build regex in my method
string valid = "^m><"; // Note 1st char is ^ (special char)
string input = ...; //some string I want to check
Check(valid);
public void Check(string valid)
{
Regex reg = new Regex("[^" + valid + "]");
if (reg.Match(input).ToString().Length > 0)
{
throw new Exception(...);
}
}
I want above Match to throw exception if it finds any other character than characters provided by valid string above. But in my case, even if I dont have any other character tahn these 3, Check method still throws new exception.
What is wrong with this regex?
this resolved it, thanks to everyone for help
Regex reg = new Regex("[^" + valid + "]", RegexOptions.IgnoreCase);
This question already has answers here:
splitting a string based on multiple char delimiters
(7 answers)
Closed 5 years ago.
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string new = test.Trim("[");
I want output "AccoutNumber".
I have tried the below code but not getting the desired result:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[');
string newvalue = test[1];
Just use Split with two delimiters:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[', ']');
string newvalue = test[1];
You can also use Regex:
string test = "Account.Parameters[\"AccountNumber\"].Caption";
var match = System.Text.RegularExpressions.Regex.Match(test, ".*?\\.Parameters\\[\"(.*?)\"]");
if (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
}
.*? is a non greedy wildcart capture, so it will match your string until it reaches the next part (in our case, it will stop at .Parameters[", match the string, and then at "])
It will match .Parameters["..."]., and extract the "..." part.
you can do a Split to that string...
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string output = test.Split('[', ']')[1];
Console.WriteLine(output);
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
String not replacing Single Quotes with required characters
string abc = "STA\'ASTEST";
if (abc.Contains("'"))
{
abc.Replace("'", "\\'");
}
You are doing the replace but not assigning the result to any variable.
I assume you want to assign the result to abc
string abc = "STA\'ASTEST";
if (abc.Contains("'"))
{
abc = abc.Replace("'", "\'");
}
It is also redundant to have the if (abc.Contains("'")) because the Replace function will only replace if the expression to replace actually exists. So you can just write:
abc = abc.Replace("'", "\'");
This question already has answers here:
RegEx Starts with [ and ending with ]
(4 answers)
Closed 6 years ago.
How to replace a set of characters where I only know the first and the last one, in between is a variable that is not constant.
All I know is that this string will always start with & and it will end with ;
string str = "Hello &145126451; mate!";
How to get rid of &145126451; ?
So the desired result is:
string result = "Hello mate!"
The most easiest way is to use Regex:
Regex yourRegex = new Regex(#"&.*;");
string result = yourRegex.Replace("Hello &145126451; mate!", String.Empty);
Console.WriteLine(result);
Here is a fiddle with example.