Unable to remove certain characters between values in c# [duplicate] - c#

This question already has answers here:
Remove text in-between delimiters in a string (using a regex?)
(5 answers)
Closed 6 years ago.
I am trying to remove characters starting from (and including) rgm up to (and including) ;1..
Example input string:
Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})
Code:
string strEx = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
strEx = strEx.Substring(0, strEx.LastIndexOf("rgm")) +
strEx.Substring(strEx.LastIndexOf(";1.") + 3);
Result:
Sum ({rgmdaerub;1.Total_Value}, {.Major_Value})
Expected result:
Sum ({Total_Value}, {Major_Value})
Note: only rgm and ;1. will remain static and characters between them will vary.

I would recommend to use Regex for this purpose. Try this:
string input = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
string result = Regex.Replace(input, #"rgm.*?;1\.", "");
Explanation:
The second parameter of Regex.Replace takes the pattern that consists of the following:
rgm (your starting string)
. (dot - meaning any character)
*? (the preceding symbol can occure zero or more times, but stops at the first possible match (shortest))
;1. (your ending string - the dot needed to be escaped, otherwise it would mean any character)

You need to use RegEx, with an expression like "rgm(.);1\.". That's just off the top of my head, you will have to verify the exact regular expression that matches your pattern. Then, use RegEx.Replace() with it.

Related

How to replace only last two sign to nothing using method Replace [duplicate]

This question already has answers here:
Replace the last occurrence of a word in a string - C#
(7 answers)
Closed last year.
I've got string "55, 55,55, 55, " and I want to replace last two characters (comma and white space) to nothing using only the method .Replace(). The example below clear all commas but I want to clear only two last ones. In Java you write:
variable.replaceAll(", $",""); // it's correct effect two last characters are changed to nothing
Replace(", ","") // but in c# you get: 55555,5555 and Replace(", $","") doesn't work
How to write it properly to get this effect using only Replace method?
Use Regex.Replace:
Regex.Replace(input, ", $", "");
If it is always two characters, you could also combine String.EndsWith and String.Substring, which is likely cheaper than compiling and applying a regex:
if (input.EndsWith(", ")) {
input = input.Substring(0, input.Length - 2);
}

Replace long dash with a usual one in C# [duplicate]

This question already has answers here:
the correct regex for replacing em-dash with a basic "-" in java
(4 answers)
Closed 3 years ago.
I have a string with multiple dashes, but it contains long dashes.
What method can I use to normalize dashes?
text = Regex.Replace(text, #"(\u2012|\u2013|\u2014|\u2015)", "-");
The expected output is something like 11-1111-11/11
The actual is almost the same, but some of the dashes are long ones. (I can't put in that dash because the stackoverflow does not recognize it.)
This works:
private const string DashPattern = #"[\u2012\u2013\u2014\u2015]";
private static Regex _dashRegex = new Regex(DashPattern);
public static string RemoveLongDashes(string s)
{
return _dashRegex.Replace(s, "-");
}
Your expression with the pipe characters (|) is not a valid regex expression. If you want to replace all of the vowels, you use an expression like #"[aeiou]", i.e., the choices within a set of square brackets.
Here is some info on the em dash. You might be able to copy and paste the dash from this post into your code and use the string.replace
The em dash
Look in the following SO post for the answer:
replacing the em dash
Looks like the following code solved the issue for others:
String s = "asd – asd";
s = s.replaceAll("\\p{Pd}", "-");

Why is this regex expression returning 'true'? [duplicate]

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
RegularExpression Validation attribute not working correctly
(2 answers)
Closed 3 years ago.
I am using this exact regex in a RegularExpressionValidator and it works fine. However this code in c# is returning true. Can you please let me know what I am doing wrong?
I am checking for numbers only regex
var x = Regex.IsMatch("1234asdf", #"[0-9]+").ToString();
Because is finding what you're looking for inside the provided string.
You have tyo use ^ and $ to force the whole string to mathc the regex:
^[0-9]+$
^ matches the beginning, $, the end of the string
Please, look ath the Anchors section of this doc: Regular Expression Language - Quick Reference
BTW, instead of [0-9] you should use \d
Other answers talk about why your regex needs to be anchored. However, nobody's picked up on the RegularExpressionValidator part of your question.
I'm assuming you mean System.Web.RegularExpressionValidator. RegularExpressionValidator does more than just checking whether the regex matches - it also checks whether the regex is an exact match. That is, it effectively adds the ^ and $ anchoring for you.
The code:
Match m = RegexUtil.Match(controlValue, ValidationExpression, RegexOptions.None, MatchTimeout);
return(m.Success && m.Index == 0 && m.Length == controlValue.Length);
Note how it checks m.Success, but also checks that the match starts at the beginning of the value being checked, and ends at the end of the value being checked.
(System.ComponentModel.DataAnnotations.RegularExpressionAttribute does the same thing).
Because you trying to match one or more digit from 0 to 9. Explaininh:
[0-9] //Match digit 0-9 you can also use /d-(Match any digit char)
+ //Match one or more of the preceding token
If you have some digit in you string, this code:
var x = Regex.IsMatch("1234asdf", #"[0-9]+").ToString();
will always return True.
For exaple if you try this one:
var x = Regex.IsMatch("asdf", #"[0-9]+").ToString();
will return False
You have to use ^ and $ to force the whole string to match the regex pattern
^[0-9]+$
^ matches the beginning, $ the end of the string
In C# Write the expression like below format:
var x = Regex.IsMatch("1234", #"^[0-9]+$").ToString();

Filter out alphabetic with regex using C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regex - Only letters?
I try to filter out alphabetics ([a-z],[A-Z]) from text.
I tried "^\w$" but it filters alphanumeric (alpha and numbers).
What is the pattern to filter out alphabetic?
Thanks.
To remove all letters try this:
void Main()
{
var str = "some junk456456%^&%*333";
Console.WriteLine(Regex.Replace(str, "[a-zA-Z]", ""));
}
For filtering out only English alphabets use:
[^a-zA-Z]+
For filtering out alphabets regardless of the language use:
[^\p{L}]+
If you want to reverse the effect remove the hat ^ right after the opening brackets.
If you want to find whole lines that match the pattern then enclose the above patterns within ^ and $ signs, otherwise you don't need them. Note that to make them effect for every line you'll need to create the Regex object with the multi-line option enabled.
try this simple way:
var result = Regex.Replace(inputString, "[^a-zA-Z\s]", "");
explain:
+
Matches the previous element one or more times.
[^character_group]
Negation: Matches any single character that is not in character_group.
\s
Matches any white-space character.
To filter multiple alpha characters use
^[a-zA-Z]+$

How do I write a regex to match a string that doesn't contain a word? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to match string not containing a word?
To not match a set of characters I would use e.g. [^\"\\r\\n]*
Now I want to not match a fixed character set, e.g. "|="
In other words, I want to match: ( not ", not \r, not \n, and not |= ).
EDIT: I am trying to modify the regex for parsing data separated with delimiters. The single-delimiter solution I got form a CSV parser, but now I want to expand it to include multi-character delimiters. I do not think lookaheads will work, because I want to consume, not just assert and discard, the matching characters.
I figured it out, it should be: ((?![\"\\r\\n]|[|][=]).)*
The full regex, modified from the CSV parser link in the original post, will be: ((?<field>((?![\"\\r\\n]|[|][=]).)*)|\"(?<field>([^\"]|\"\")*)\")([|][=]|(?<rowbreak>\\r\\n|\\n|$))
This will match any amount of characters of ( not ", not \r, not \n, and not |= ), or a quoted string, followed by ( "|=" or end of line )

Categories

Resources