Extracting a string between two characters - c#

How do I extract a string between two characters using a regular expression?
For example:
{sdjhjkfd 78983njdsjnc cxjkc/m/xc;dfo}/mnvckjdf{jhdfkjhxbbnkhfd}
How to get string between { and }?

Regex regexObj = new Regex(#"(?<=\{)[^{}]*(?=\})");
allMatchResults = regexObj.Matches(subjectString);
gets you all text between innermost, correctly balanced braces. No escaped braces are allowed.

Related

Regular Expression for a middle string

I need to extract from the below string
2_240219_0.vnd as 240219
I have tried as follows: _[0-9]+_
This gives me _240219_
How do I remove the _ from both ends.
I would actually recommend not even using regex in this case. A simple string split on underscore should do just fine:
string input = "2_240219_0.vnd";
string middle = input.Split('_')[1];
Console.WriteLine(middle);
240219
You can try using a other regex: ([\d]{6,})
Match m = Regex.Match(2_240219_0.vnd, `([\d]{6,})`, RegexOptions.IgnoreCase);

C# Regex search string for text including surrounding brackets

I would like to search a string for '[E1010]' or '[E1011]' or '[E1012]'. Currently, I can only successfully search without using the brackets []. How can I adjust my regex to include the texting surrounded by the brackets as it is in my sClientError variable.
Thanks!
string sClientErrors = "Bla Blah \"30\" [E1011]\r\nBlah Blah"44\" [E1012]";
Regex myRegexE10 = new Regex(#"\bE1010\b");
Regex myRegexE11 = new Regex(#"\bE1011\b");
Regex myRegexE12 = new Regex(#"\bE1012\b");
if (myRegexE10.IsMatch(sClientErrors) || myRegexE11.IsMatch(sClientErrors) || myRegexE12.IsMatch(sClientErrors))
{
// do code here...
}
By adding the brackets:
Regex myRegexE10 = new Regex(#"\[E1010]");
or
Regex myRegexE1x = new Regex(#"\[E101[012]]");
if (myRegexE1x.IsMatch(sClientErrors)) { ...
Note that once you add the brackets, word boundaries are no longer necessary. Note too that you don't need to escape closing square brackets
You can put a "\" if front of a character you want to include, so you would use:
Regex myRegexE10 = new Regex(#"\[\bE1010\b\]")
You can also use "\\" if you needed to find something like "\s", where "\*" is a Regex option.

How to select a group of characters in a string using regular expression

I am new to regular expression. I am trying to match a group of characters using regular expression, but it does not work.
Here is my code.
string test = "Hello$#%$all";
string regex = "($#%$)";
string result = Regex.Replace(test, regex, "\n");
Any help??
You need to escape the characters that has special meaning in regular expressions.
string test = "Hello$#%$all";
string regex = #"\$#%\$";
string result = Regex.Replace(test, regex, "\n");
Characters like $ has special meaning when used in a regular expression. So tell apart whether it's a character used to signify something in the expression or whether you need to literally match that character, you can escape it using a \

string manipulation in regex

i have a problem in string manipulation
here is the code
string str = "LDAP://company.com/OU=MyOU1 Control,DC=MyCompany,DC=com";
Regex regex = new Regex("OU=\\w+");
var result = regex.Matches(str);
var strList = new List<string>();
foreach (var item in result)
{
strList.Add(item.ToString().Remove(0,3));
}
Console.WriteLine(string.Join("/",strList));
the result i am getting is "MyOU1" instead of getting "MyOU1 Control"
please help thanks
If you want the space character to be matched as well, you need to include it in your regex. \w only matches word charactes, which does not include spaces.
Regex regex = new Regex(#"OU=[\w\s]+");
This matches word characters (\w) and whitespace characters (\s).
(The # in front of the string is just for convenience: If you use it, you don't need to escape backslashes.)
Either add space to the allowed list (\w doesn't allow space) or use the knowledge that comma can be used as a separator.
Regex regex = new Regex("OU=(\\w|\\s)+");
OR
Regex regex = new Regex("OU=[^,]+");

convert unicode escape sequences to string

Hi I have this problem. From server I get JSON string as unicode escape sequences an I need convert this sequences to unicode string. I find some solution, but any doesn’t work for all json response.
For example from server I get this string.
string encodedText="{\"DATA\":{\"idUser\":18167521,\"nick\":\"KecMessanger2\",\"photo\":\"1\",\"sex\":1,\"photoAlbums\":0,\"videoAlbums\":0,\"sefNick\":\"kecmessanger2\",\"profilPercent\":0,\"emphasis\":false,\"age\":25,\"isBlocked\":false,\"PHOTO\":{\"normal\":\"http://213.215.107.125/fotky/1816/75/n_18167521.jpg?v=1\",\"medium\":\"http://213.215.107.125/fotky/1816/75/m_18167521.jpg?v=1\",\"24x24\":\"http://213.215.107.125/fotky/1816/75/s_18167521.jpg?v=1\"},\"PLUS\":{\"active\":false,\"activeTo\":\"0000-00-00\"},\"LOCATION\":{\"idRegion\":\"1\",\"regionName\":\"Banskobystricku00fd kraj\",\"idCity\":\"109\",\"cityName\":\"Rimavsku00e1 Sobota\"},\"STATUS\":{\"isLoged\":true,\"isChating\":false,\"idChat\":0,\"roomName\":\"\",\"lastLogin\":1291898043},\"PROJECT_STATUS\":{\"photoAlbums\":0,\"photoAlbumsFavs\":0,\"videoAlbums\":0,\"videoAlbumsFavs\":0,\"videoAlbumsExts\":0,\"blogPosts\":0,\"emailNew\":0,\"postaNew\":0,\"clubInvitations\":0,\"dashboardItems\":26},\"STATUS_MESSAGE\":{\"statusMessage\":\"Nepru00edtomnu00fd.\",\"addTime\":\"1291887539\"},\"isFriend\":false,\"isIamFriend\":false}}";
statusMessage in jsonstring consist Nepru00edtomnu00fd, in .net unicode string is it Neprítomný.
region in jsonstring consist Banskobystricku00fd in .net unicode string is it BanskoBystrický.
Other examples:
Nepru00edtomnu00fd -> Neprítomný
Banskobystricku00fd -> BanskoBystrický
Trenu010du00edn -> Trenčín
I need convert unicode escape sequences to .net string in slovak language.
On converting I used this func:
private static string UnicodeStringToNET(string input)
{
var regex = new Regex(#"\\[uU]([0-9A-F]{4})", RegexOptions.IgnoreCase);
return input = regex.Replace(input, match => ((char)int.Parse(match.Groups[1].Value,
NumberStyles.HexNumber)).ToString());
}
Where can be problem?
Here's a method (based on previous answers) that I wrote to do the job. It handles both \uhhhh and \Uhhhhhhhh, and it will preserve escaped unicode escapes (so if your string needs to contain a literal \uffff, you can do that). The temporary placeholder character \uf00b is in a private use area, so it shouldn't typically occur in Unicode strings.
public static string ParseUnicodeEscapes(string escapedString)
{
const string literalBackslashPlaceholder = "\uf00b";
const string unicodeEscapeRegexString = #"(?:\\u([0-9a-fA-F]{4}))|(?:\\U([0-9a-fA-F]{8}))";
// Replace escaped backslashes with something else so we don't
// accidentally expand escaped unicode escapes.
string workingString = escapedString.Replace("\\\\", literalBackslashPlaceholder);
// Replace unicode escapes with actual unicode characters.
workingString = new Regex(unicodeEscapeRegexString).Replace(workingString,
match => ((char) Int32.Parse(match.Value.Substring(2), NumberStyles.HexNumber))
.ToString(CultureInfo.InvariantCulture));
// Replace the escaped backslash placeholders with non-escaped literal backslashes.
workingString = workingString.Replace(literalBackslashPlaceholder, "\\");
return workingString;
}
Your escape sequences do not start with a \ like "\u00fd" so you Regex should be only
"[uU]([0-9A-F]{4})"
...

Categories

Resources