c# Extract String By Regex - c#

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

Related

How to allow space in regex?

I am trying to get the value after New : in double quote.
I can retrieve the value fine when there is no space in ListName. But if I put space between the list name (eg. NewFinancial History:\"xyz\"), it throws the error below:
parsing "NewFinancial History:"(?[^"]*)"" - Invalid group name: Group names must begin with a word character.
it throws error at below line
var matches = Regex.Matches(contents, regex, RegexOptions.Singleline);
Below is my code.
string contents = " testing NewFinancial History:\"xyz\" ";
var keys = Regex.Matches(contents, #"New(.+?):", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace).OfType<Match>().Select(m => m.Groups[0].Value.Trim().Replace(":", "")).Distinct().ToArray();
foreach (string key in keys)
{
List<string> valueList = new List<string>();
string listNameKey = key;
string regex = "" + listNameKey + ":" + "\"(?<" + listNameKey + ">[^\"]*)\"";
var matches = Regex.Matches(contents, regex, RegexOptions.Singleline);
foreach (Match match in matches)
{
if (match.Success)
{
string value = match.Groups[key].Value;
valueList.Add(value);
}
}
}
I don't see why you also use the "key" as name of the group.
The problem you have is that the group name
could not contain spaces, but you could simply create an anonymous group.
string contents = " testing NewFinancial History:\"xyz\" ";
var keys = Regex.Matches(contents, #"New(.+?):", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace).OfType<Match>().Select(m => m.Groups[0].Value.Trim().Replace(":", "")).Distinct().ToArray();
foreach (string key in keys)
{
List<string> valueList = new List<string>();
string listNameKey = key;
string regex = "" + listNameKey + ":" + "\"([^\"]*)\""; //create an anonymous capture group
var matches = Regex.Matches(contents, regex, RegexOptions.Singleline);
foreach (Match match in matches)
{
if (match.Success)
{
string value = match.Groups[0].Value; //get the first group
valueList.Add(value);
}
}
}
Change your foreach block to
List<string> valueList = new List<string>();
string listNameKey = key;
string regex = "" + listNameKey + ":" + "\"(?<" +
listNameKey.Replace(" ","") + ">[^\"]*)\""; // Removing spaces in the group name here
var matches = Regex.Matches(contents, regex, RegexOptions.Singleline);
foreach (Match match in matches)
{
if (match.Success)
{
string value = match.Groups[key.Replace(" ", "")].Value; // Removing spaces here
valueList.Add(value);
}
}
The point is that group names cannot have whitespace, so you need to replace them with empty strings in places where you declare the capture group name.
See IDEONE demo
Note that your New(.+?): regex has no whitespace to ignore, I recommend deleting RegexOptions.IgnorePatternWhitespace flag. You can replace it with a more efficient New([^:]+):.

Regex formatting metacharacters

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);

Replacement in a String with a regular expression

I'm trying to replace a string in C# with the class Regex but I don't know use the class properly.
I want replace the next appearance chain in the String "a"
":(one space)(one or more characters)(one space)"
by the next regular expression
":(two spaces)(one or more characters)(three spaces)"
Will anyone help me and give me the code and explains me the regular expresion used?
you can use string.Replace(string, string)
try this one.
http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
try this one
private String StrReplace(String Str)
{
String Output = string.Empty;
String re1 = "(:)( )((?:[a-z][a-z]+))( )";
Regex r = new Regex(re1, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(Str);
if (m.Success)
{
String c1 = m.Groups[1].ToString();
String ws1 = m.Groups[2].ToString() + " ";
String word1 = m.Groups[3].ToString();
String ws2 = m.Groups[4].ToString() + " ";
Output = c1.ToString() + ws1.ToString() + word1.ToString() + ws2.ToString() + "\n";
Output = Regex.Replace(Str, re1, Output);
}
return Output;
}
Using String.Replace
var str = "Test string with : .*. to replace";
var newstr = str.Replace(": .*. ", ": .*. ");
Using Regex.Replace
var newstr = Regex.Replace(str,": .*. ", ": .*. ");

Get Regex occurance with escaped symbols

I would appreciate help with non-working regex (does not work for special symbols % or $)
public System.Tuple<string, string> GetParts(string str, string beginMark, string endMark)
{
var pattern =
new Regex(beginMark + #"(?<val>.*?)" + endMark,
RegexOptions.Compiled |
RegexOptions.Singleline);
return (from Match match in pattern.Matches(str)
where match.Success
select new Tuple(
match.Value,
match.Groups["val"].Value))
.ToList();
}
Calling method:
string input = #"%sometext%\another text";
string replacedValue = "AAA";
var occurrences = GetPart(input, #"(%", ")");
foreach (var occurrence in occurrences)
{
Console.WriteLine(occurrence.Item1 + Environment.NewLine);
Console.WriteLine(occurrence.Item2 + Environment.NewLine);
// replace
onsole.WriteLine(input.Replace(occurrence.Item1, replacedValue) + Environment.NewLine);
}
Expected Output:
%sometext%
sometext
AAA\another text
You need to escape your symbols. Try to change
new Regex(beginMark + #"(?<val>.*?)" + endMark,
to
new Regex(Regex.Escape(beginMark) + #"(?<val>.*?)" + Regex.Escape(endMark),

Regex Replace on a JSON structure

I am currently trying to do a Regex Replace on a JSON string that looks like:
String input = "{\"`####`Answer_Options11\": \"monkey22\",\"`####`Answer_Options\": \"monkey\",\"Answer_Options2\": \"not a monkey\"}";
a
The goal is to find and replace all the value fields who's key field starts with `####`
I currently have this:
static Regex _FieldRegex = new Regex(#"`####`\w+" + ".:.\"(.*)\",");
static public string MatchKey(string input)
{
MatchCollection match = _encryptedFieldRegex.Matches(input.ToLower());
string match2 = "";
foreach (Match k in match )
{
foreach (Capture cap in k.Captures)
{
Console.WriteLine("" + cap.Value);
match2 = Regex.Replace(input.ToLower(), cap.Value.ToString(), #"CAKE");
}
}
return match2.ToString();
}
Now this isn't working. Naturally I guess since it picks up the entire `####`Answer_Options11\": \"monkey22\",\"`####`Answer_Options\": \"monkey\", as a match and replaces it. I want to just replace the match.Group[1] like you would for a single match on the string.
At the end of the day the JSON string needs to look something like this:
String input = "{\"`####`Answer_Options11\": \"CATS AND CAKE\",\"`####`Answer_Options\": \"CAKE WAS A LIE\",\"Answer_Options2\": \"not a monkey\"}";
Any idea how to do this?
you want a positive lookahead and a positive lookbehind :
(?<=####.+?:).*?(?=,)
the lookaheads and lookbehinds will verify that it matches those patterns, but not include them in the match. This site explains the concept pretty well.
Generated code from RegexHero.com :
string strRegex = #"(?<=####.+?:).*?(?=,)";
Regex myRegex = new Regex(strRegex);
string strTargetString = #" ""{\""`####`Answer_Options11\"": \""monkey22\"",\""`####`Answer_Options\"": \""monkey\"",\""Answer_Options2\"": \""not a monkey\""}""";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
this will match "monkey22" and "monkey" but not "not a monkey"
Working from #Jonesy's answer I got to this which works for what I wanted. It includes the .Replace on the groups that I required. The negative look ahead and behinds were very interesting but I needed to replace some of those values hence groups.
static public string MatchKey(string input)
{
string strRegex = #"(__u__)(.+?:\s*)""(.*)""(,|})*";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
IQS_Encryption.Encryption enc = new Encryption();
int count = 1;
string addedJson = "";
int matchCount = 0;
foreach (Match myMatch in myRegex.Matches(input))
{
if (myMatch.Success)
{
//Console.WriteLine("REGEX MYMATCH: " + myMatch.Value);
input = input.Replace(myMatch.Value, "__e__" + myMatch.Groups[2].Value + "\"c" + count + "\"" + myMatch.Groups[4].Value);
addedJson += "c"+count + "{" +enc.EncryptString(myMatch.Groups[3].Value, Encoding.UTF8.GetBytes("12345678912365478912365478965412"))+"},";
}
count++;
matchCount++;
}
Console.WriteLine("MAC" + matchCount);
return input + addedJson;
}`
Thanks again to #Jonesy for the huge help.

Categories

Resources