Regex formatting metacharacters - c#

I need help to remove the characters in this line(the ^ & and \) and also the empty values in between them, im new to regex
1H|\^&|||ARCH^8.10^F3453010030^H1P1O1R1C1Q1L1|||||||P|1|20150511083525
1D
it should be:
1H|ARCH|8.10|F3453010030|H1P1O1R1C1Q1L1|P|20150511083525|1D

For the given string, you can use the following to replace those characters.
String result = Regex.Replace(input, #"[^\w.]+", "|");

[\^&\\|]+
Try this.Replace by |.See demo.
https://regex101.com/r/oF9hR9/6
string strRegex = #"[\^&\\|]+";
Regex myRegex = new Regex(strRegex, RegexOptions.Multiline);
string strTargetString = #"1H|\^&|||ARCHITECT^8.10^F3453010030^H1P1O1R1C1Q1L1|||||||P|1|20150511083525" + "\n" + #"1D";
string strReplace = #"|";
return myRegex.Replace(strTargetString, strReplace);

Related

Regex for slitting string by pipe skips empty strings

This is my regex:
var separator = '|';
Regex csvSplit = new Regex("(?:^|" + separator + ")(\"(?:[^\"]+|\"\")*\"|[^" + separator + "]+)", RegexOptions.Compiled);
var test = csvSplit.Matches("10734|Vls, p|6||1.5");
As you can see, there is one empty record.
This is what I get:
I was expecting empty string on index 3, but instead it is skipped. what am I doing wrong?
Try this regex instead:
(?:^|(?<=\|))((?:"[^"]*"|[^|])*)(?=\||$)

Removing special characters from a string with RegEx

Am reading a text file that contains words, numbers and special characters, I want to remove certain special characters like: [](),'
I have this code but it is not working !
using (var reader = new StreamReader ("C://Users//HP//Documents//result2.txt")) {
string line = reader.ReadToEnd ();
Regex rgx = new Regex ("[^[]()',]");
string res = rgx.Replace (line, "");
Message1.text = res;
}
what am I missing, thanks
Some of the characters in your Regex, specifically [ ] ( ) ^, hold special meaning in Regex and in order to use them literally they must be escaped.
Use the following properly escaped Regex:
Regex rgx = new Regex (#"[\^\[\]\(\)',]");
Note that it is necessary to use the # verbatim string, because we don't want to escape these characters from the string, only from the Regex.
Alternatively, double escape the backslashes:
Regex rgx = new Regex ("[\\^\\[\\]\\(\\)',]");
But that's less readable in this case.
You could skip Regex and just maintain a list of characters you want to remove and then replace the old fashioned way:
string[] specialCharsToRemove = new [] { "[", "]", "(", ")", "'", "," };
using (var reader = new StreamReader ("C://Users//HP//Documents//result2.txt"))
{
string line = reader.ReadToEnd();
foreach(string s in specialCharsToRemove)
{
line = line.Replace(s, string.Empty);
}
Message1.text = res;
}
Ideally this would be in its own method, something like this:
private static string RemoveCharacters(string input, string[] specialCharactersToRemove)
{
foreach(string s in specialCharactersToRemove)
{
input = input.Replace(s, string.Empty);
}
return input;
}
I made a fiddle here
Replace them one at a time with String.Replace:
using (var reader = new StreamReader ("C://Users//HP//Documents//result2.txt"))
{
string line = reader.ReadToEnd ();
string res = line.Replace(line, "[", "");
res = res.Replace(line, "]", "");
res = res.Replace(line, "(", "");
res = res.Replace(line, ")", "");
res = res.Replace(line, "'", "");
res = res.Replace(line, ",", "");
Message1.text = res;
}
I agree with avoiding regex for this, but I would not use string.Replace multiple times, either.
Consider implementing a Replace or Remove method that accepts an array of characters to replace, and scan the input string only once. For example:
var builder = new StringBuilder();
foreach (char ch in input)
{
if (!chars.Contains(ch))
{
builder.Append(ch):
}
}
return builder.ToString();

c# Extract String By Regex

i use Regex extract string text here ..
+CMGR: "REC UNREAD","MSG","","2013/11/04 14:17:43+28" 0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E19
i need select text
"0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E19"
i use code :
string strRegex = #"\b+CMGR: w+\n(\w+)\b";
RegexOptions myRegexOptions = RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"+CMGR: ""REC UNREAD"",""MSG"","""",""2013/11/04 13:52:18+28""" + "\r\n" + #"0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E197";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
return myRegex.Split(strTargetString);
}
}
is can't extract it..
Thank you
To find last line you may use \w+$ pattern:
string strRegex = #"\w+$";
Regex myRegex = new Regex(strRegex);
string strTargetString = #"+CMGR: ""REC UNREAD"",""MSG"","""",""2013/11/04 13:52:18+28""" + "\r\n" + #"0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E197";
return myRegex.Match(strTargetString);
. But the simplest way is to split string into lines and select the second one:
strTargetString.Split('\n')[1]
Your code will work with this patter: "^+.+\s"
void Main()
{
string strRegex = #"^\+.+\s";
RegexOptions myRegexOptions = RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"+CMGR: ""REC UNREAD"",""MSG"","""",""2013/11/04 13:52:18+28""" + "\r\n" + #"0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E197";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
Console.WriteLine(myRegex.Split(strTargetString));
}
}
}
output
0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E197

Extract some numbers and decimals from a string

I have a string:
" a.1.2.3 #4567 "
and I want to reduce that to just "1.2.3".
Currently using Substring() and Remove(), but that breaks if there ends up being more numbers after the pound sign.
What's the best way to go about doing this? I've read a bunch of questions on regex & string.split, but I can't get anything I try to work in VB.net. Would I have to do a match then replace using the match result?
Any help would be much appreciated.
This should work:
string input = " a.1.2.3 #4567 ";
int poundIndex = input.IndexOf("#");
if(poundIndex >= 0)
{
string relevantPart = input.Substring(0, poundIndex).Trim();
IEnumerable<Char> numPart = relevantPart.SkipWhile(c => !Char.IsDigit(c));
string result = new string(numPart.ToArray());
}
Demo
Try this...
String[] splited = split("#");
String output = splited[0].subString(2); // 1 is the index of the "." after "a" considering there are no blank spaces before it..
Here is regex way of doing it
string input = " a.1.2.3 #4567 ";
Regex regex = new Regex(#"(\d\.)+\d");
var match = regex.Match(input);
if(match.Success)
{
string output = match.Groups[0].Value;//"1.2.3"
//Or
string output = match.Value;//"1.2.3"
}
If the pound sign is the most relevant bit, rely on Split. Sample VB.NET code:
Dim inputString As String = " a.1.2.3 #4567 "
If (inputString.Contains("#")) Then
Dim firstBit As String = inputString.Split("#")(0).Trim()
Dim headingToRemove As String = "a."
Dim result As String = firstBit.Substring(headingToRemove.Length, firstBit.Length - headingToRemove.Length)
End If
As far as this is a multi-language question, here comes the translation to C#:
string inputString = " a.1.2.3 #4567 ";
if (inputString.Contains("#"))
{
string firstBit = inputString.Split('#')[0].Trim();
string headingToRemove = "a.";
string result = firstBit.Substring(headingToRemove.Length, firstBit.Length - headingToRemove.Length);
}
I guess another way using unrolled
\d+ (?: \. \d+ )+

Remove 2 or more blank spaces from a string in c#..?

what is the efficient mechanism to remove 2 or more white spaces from a string leaving single white space.
I mean if string is "a____b" the output must be "a_b".
You can use a regular expression to replace multiple spaces:
s = Regex.Replace(s, " {2,}", " ");
Something like below maybe:
var b=a.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
var noMultipleSpaces = string.Join(" ",b);
string tempo = "this is a string with spaces";
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(#"[ ]{2,}", options);
tempo = regex.Replace(tempo, #" ");
You Can user this method n pass your string value as argument
you have to add one namespace also using System.Text.RegularExpressions;
public static string RemoveMultipleWhiteSpace(string str)
{
// A.
// Create the Regex.
Regex r = new Regex(#"\s+");
// B.
// Remove multiple spaces.
string s3 = r.Replace(str, #" ");
return s3;
}

Categories

Resources