Best way to remove unknown characters and spaces using C#? [duplicate] - c#

This question already has answers here:
How can I remove the spaces, tabs, new lines between characters using c#'s REGEX?
(2 answers)
Closed 6 years ago.
Unknown Characters:
|b9-12-2016,¢Xocoak¡LO2A35(2)(b)¡ÓocORe3ao-i|],¢Xa?u¡±o¡±i?¢X$3,597,669On 9-12-2016, the price adjusted to $3,597,669 dueto the reason allowed under section 35(2)(b) of theOrdinance
Good Result:
$3,597,669On 9-12-2016, the price adjusted to $3,597,669 due to the reason allowed under section 35 of the Ordinance

You should be able to use regular expressions to do this. You can use the Regex.Replace method to run regular expressions on your text. Regular expressions are patterns that a regular expression engine tries to match in input text. I recommend that you take a look at the MSDN article here. You can also take a look at the documentation for the Regex.Replace method here. For example, in order to remove the letter c you could use this snippet of code:
output = Regex.Replace(input, "c", "", RegexOptions.IgnoreCase);
This would replace both lowercase and capital Cs because the ignore case option is turned on.

If it is a standard pattern as what you've told me. Use the following code. It takes everything after the last $ sign.
string str = "|b9-12-2016,¢Xocoak¡LO2A35(2)(b)¡ÓocORe3ao-i|],¢Xa?u¡±o¡±i?¢X$3,597,669On 9-12-2016, the price adjusted to $3,597,669 dueto the reason allowed under section 35(2)(b) of theOrdinance";
var result = str.Substring(str.LastIndexOf('$'));

Related

.Net Regex - trying to locate a one liner to convert the case of fully qualified server names [duplicate]

This question already has answers here:
Use C# regex to convert casing in a string
(3 answers)
Use RegEx to uppercase and lowercase the string
(2 answers)
Closed 4 years ago.
Trying to answer myself an academic exercise here.
Is there a method using Regular Expressions (.net syntax, so see the caveat below) that I can convert fully qualified server name to a combo upper and lower case string (server name is UPPER case, domain name(s) in lower case).
e.g.
db01.local => DB01.local
DB02.TEST.LOCAL => DB02.test.local
db03.LOCAL = > DB03.local
I've been playing around with the RE and so far have ([A-Za-z0-9-]+)\.(.+) as the pattern, but I'm struggling how to do this in a simple one liner.
My initial tests had me fritzing with Matches and getting a returned list, but that feels fugly to me because I then need to check the number of matches, do casting and ToUpper() \ ToLower() operations etc. and, yeah, well...
Caveat: If I wasn't using .NET then I think I should be able to do something simple like use \U${1}.\L${2} as my replacement string, but it doesn't look like .NET supports that syntax.
Using the 'Possible duplicate of' link this is what I ended up with:
Regex re = new Regex(#"([A-Za-z0-9-]+)\.(.+)");
foreach (var i in _knownServers)
clean.Add(re.Replace(i, m => $"{m.Groups[1].ToString().ToUpper()}.{m.Groups[2].ToString().ToLower()}"));

Regular expression for characters after '.' [duplicate]

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
Closed 6 years ago.
I need to detect following format when I enter serial number like
CK123456.789
I used Regex with pattern of
^(CV[0-9]{6}\.[0-9]{3}
to match but if I enter
CK123456.7890
it still able to proceed without flagging error. Is there a better regular expression to detect the trailing 3 digits after '.'?
Depending on how you use the regular expression matcher, you might need to enclose it in ^...$ which forces the pattern to be the whole string, i.e.
^CK[0-9]{6}\.[0-9]{3}$ (Note the CK prefix).
I've also removed your leading (mismatched) parenthesis.

Need to get so substring but using Regular Expression [duplicate]

This question already has answers here:
C# Substring Alternative - Return rest of line within string after character
(5 answers)
Closed 6 years ago.
I got on url like.
http://EddyFox.com/x/xynua
Need to fetch substring after /x/ what ever string is there.
complex example I faced is :
http://EddyFox.com/x//x/
Here result should be /x/
It can be achieved with substring ,But we need to perform it with regular expression.
This should do it:
string s = "http://EddyFox.com/x/xynua";
// I guess you don't want the /x/ in your match ?=!
Console.WriteLine(Regex.Match(s, "/x/(.*)").Groups[1].Value );
this is probably even better:
Console.WriteLine(Regex.Match(s, "(?<=/x/)(.*)").Value );
the output is
xynua
Have a look at this post: Regex to match after specific characters SO is full of RegEx posts. The probability is very high that a RegEx question has already been asked before. :)
The regex /x/(.*) will capture everything following the /x/
And where is the problem?
var r = new Regex("/x/(\\S*)");
var matches = r.Matches(myUrl);
This regex matches everything from /x/ until the first occurence of a white-space.

Matching a comment (//Comment) in regex outside quotation marks [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regex to strip line comments from C#
I'm completely stuck with this, and i'm not good at making regex.
Basicly i want to match comments in pieces of text, for example this one:
//Comment outside quotations
string text = "//Comment inside quotations..";
//Another comment
I want only the top and bottom comment to match, but not the middle one inside quotations
What i have now for comments is:
//.*$
To match a comment throughout the end of the line.
What i want this to use for is for syntax highlighting in a textBox.
Is this possible to do?
Try this :
"^(?!\".*\")//.*$"
This will match
//Comment outside quotations
and will not match
string text = "//Comment inside quotations..";
Please make required escaping for c#
Try this regex:
([^"]|"[^"]*")*(?<COMMENT>//.*)
Parse each match for the named group "COMMENT" (or whatever you choose to name it). Quick disclaimer that I didn't test it out in C#, I just threw the regex together using an online tool.

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]+$

Categories

Resources