get text inside quotes using regex - c#

I want to extract the data inside the quotes using regex
My Text is : boundary="s323sd2342423---"
Now i need to extract the value inside the double quotes without using substring.
I tried the following but didnt help.
String pattern = #"boundary=""(?<value>[^""]*";
Match m = Regex.Match(rawMessage, pattern);
while (m.Success)
{
boundaryString = m.Groups["value"].Value;
m = m.NextMatch();
}

You need to close opening bracket of a group
String pattern = #"boundary=""(?<value>[^""]*)";
now if you go with
Console.WriteLine(m.Groups["value"].Value);
will print:
s323sd2342423---

You can use this pattern and it will work.
String pattern = #"boundary=\""(?<value>.+?)\""";

With the following Regex you'll get what you want without any grouping
(?<=boundary=")[^"]+(?=")
Code to get the the quoted text:
string txt = "boundary=\"s323sd2342423---\"";
string quotedTxt = Regex.Match(txt, #"(?<=boundary="")[^""]+(?="")").Value;

As per you details:
I want to extract the data inside the quotes using regex
Why not you just use this pattern:
"(?<value>[^"]+)"

Related

Trying to regex a string with backslashes and quotes

I am trying to regex a string in csharp. I am expecting to pass a string with the following format:
<%=Application(\"DisplayName\")%>
and get back:
DisplayName
I am using the regex class to accomplish this:
var text = "<%=Application(\"DisplayName\")%>";
Regex regex = new Regex(#"(<\%=Application[\>\(\)][\\][""](.*?)[\\][""][\>\(\)k]%\>)");
var v = regex.Match(text);
var s = v.Groups[1].ToString();
I am expecting s to contain the output string, but it is coming back as "". I tried building the regex string step by step, but I can't get the \ or " to process correctly. Any help would be greatly appreciated. Thanks!
var text = "<%=Application(\"DisplayName\")%>";
Regex regex = new Regex(#"(<%=Application[>()][""](.*?)[""][>()k]%>)");
var v = regex.Match(text);
var s = v.Groups[1].ToString();
Your pattern is very close. Since the backslashes are not actually a part of the string, rather only in the string to escape the double quotes, they need to be left out of the regex pattern. Notice I removed the [\\] from before both of the double quotes [""].
Now, you expect DisplayName in Group[1]. Since Regex sticks the entire match in Group[0], that made your outer capture group (whole pattern in parenthesis) the first actual capture group (Making DisplayName actually Group[2]). For best practice, I changed the outer capture group to be a non-capture group by adding ?: to the open parenthesis. This ignores this full group and makes DisplayName Group[1]. Hope this helps.
Full test code:
var text = "<%=Application(\"DisplayName\")%>";
Regex regex = new Regex(#"(?:<\%=Application[\>\(\)][""](.*?)[""][\>\(\)k]%\>)");
var v = regex.Match(text);
var s = v.Groups[1].ToString();

Replacing a portion of a string with an exact matching

I just want to replace a portion of a string only if matches the given text.
My use case is as follows:
var text = "<wd:response><wd:response-data></wd:response-data></wd:response >";
string result = text.Replace("wd:response", "response");
/*
* expecting the below text
<response><wd:response-data></wd:response-data></response>
*
*/
I followed the following answers:
Way to have String.Replace only hit "whole words"
Regular expression for exact match of a string
But I failed to achieve what I want.
Please share your thoughts/solutions.
Sample on
https://dotnetfiddle.net/pMkO8Q
In general, you should really be parsing and manipulating XML as XML, using functions that know how XML works and what's legal in the language. Regex and other naive text manipulation will often lead you into trouble.
That said, for a very simple solution to this specific problem, you can do this with two replaces:
var text = "<wd:response><wd:response-data></wd:response-data></wd:response >";
text.Replace("wd:response>", "response>").Replace("wd:response ", "response ")
(Note the spaces at the end of the parameters to the second replace.)
Alternatively use a regex similar to "wd:response\s*>"
The easiest way to achieve your result as per your .net fiddle is use the replace as below.
string result = text.Replace("wd:response>", "response>");
But proper way to achieve this is parsing using XML
You can capture the string wd-response in a capturing group and replace using Regex.Replace using the MatchEvaluator like this.
Regex explanation - <[/]?(wd:response)[\s+]?>
Match < literally
Match / optionally hence the ?
Match the string wd:response and place it in a capturing group enclosed with ()
Match one or more optional whitespace [\s+]?
Match > literally
public class Program
{
public static void Main(string[] args)
{
string text = "<wd:response><wd:response-data></wd:response-data></wd:response >";
string replacePattern = "response";
string pattern = #"<[/]?(wd:response)[\s+]?>";
string replacedPattern = Regex.Replace(text, pattern, match =>
{
// Extract the first group
Group group = match.Groups[1];
// Replace the group value with the replacePattern
return string.Format("{0}{1}{2}", match.Value.Substring(0, group.Index - match.Index), replacePattern, match.Value.Substring(group.Index - match.Index + group.Length));
});
Console.WriteLine(replacedPattern);
}
}
Outputting:
<response><wd:response-data></wd:response-data></response >

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

Regex within a regex?

Truth is, I'm having a hard time writing a regex string to parse something in the form of
[[[tab name=dog content=cat|tab name=dog2 content=cat2]]]
This regex would be parsed so that I can dynamically build tabs as demonstrated here. Initially I tried a regex pattern like \[\[\[tab name=(?'name'.*?) content=(?'content'.*?)\]\]\]
But I realized I couldn't get the tab as a whole and build upon a query without doing a regex.replace. Is it possible to take the entire tab leading up to the pipe symbol as a group and then parse that group down from the sub key/value pairs?
This is the current regex string I'm working with \[\[\[(?'tab'tab name=(?'name'.*?) content=(?'content'.*?))\]\]\]
And here is my code for performing the regex. Any guidance would be appreciated.
public override string BeforeParse(string markupText)
{
if (CompiledRegex.IsMatch(markupText))
{
// Replaces the [[[code lang=sql|xxx]]]
// with the HTML tags (surrounded with {{{roadkillinternal}}.
// As the code is HTML encoded, it doesn't get butchered by the HTML cleaner.
MatchCollection matches = CompiledRegex.Matches(markupText);
foreach (Match match in matches)
{
string tabname = match.Groups["name"].Value;
string tabcontent = HttpUtility.HtmlEncode(match.Groups["content"].Value);
markupText = markupText.Replace(match.Groups["content"].Value, tabcontent);
markupText = Regex.Replace(markupText, RegexString, ReplacementPattern, CompiledRegex.Options);
}
}
return markupText;
}
Is this what you want?
string input = "[[[tab name=dog content=cat|tab name=dog2 content=cat2]]]";
Regex r = new Regex(#"tab name=([a-z0-9]+) content=([a-z0-9]+)(\||])");
foreach (Match m in r.Matches(input))
{
Console.WriteLine("{0} : {1}", m.Groups[1].Value, m.Groups[2].Value);
}
http://regexr.com/3boot
Maybe string.split will be better in that case? For example something like that :
strgin str = "[[[tab name=dog content=cat|tab name=dog2 content=cat2]]]";
foreach(var entry in str.Split('|')){
var eqBlocks = entry.Split('=');
var tabName = eqBlocks[1].TrimEnd(" content");
var content = eqBlocks[2];
}
Ugly code, but should work.
Try this:
Starts with a word boundary and followed only by allowed characters.
/\b[\w =]*/g
https://regex101.com/r/cI7jS7/1
Just distill the regex pattern down to the individual tab patterns such as name=??? content=??? and match that only. That pattern which will make each Match (two in you example) where the data can be extracted.
string text = #"[[[tab name=dog content=cat|tab name=dog2 content=cat2]]]";
string pattern = #"name=(?<Name>[^\s]+)\scontent=(?<Content>[^\s|\]]+)";
var result = Regex.Matches(text, pattern)
.OfType<Match>()
.Select(mt => new
{
Name = mt.Groups["Name"].Value,
Content = mt.Groups["Content"].Value,
});
The result is an enumerable list with the created dynamic entities with the tabs needed which can be directly bound to the control:
Note in the set notation [^\s|\]] the pipe | is treated as a literal in the set and not used as an or. The bracket ] does have to be escaped though to be treated as a literal. Finally the logic the parse will look for: "To not (^) be a space or a pipe or a brace for that set".

Parse and replace string using Regex

i have various strings that look like that:
$(gateway.jms.jndi.ic.url,0,tibjmsnaming, tcp)/topic/$(gateway.destination.prefix)$(gateway.StatusTopicName),$(gateway.jms.jndi.ic.username),$(gateway.jms.jndi.ic.password),abinding,tBinding
i'm trying to figure out a way to extract the $(...) sections and replace them with some other string.
is there anyway in C# to parse those groups and replace one by one with another string?
Thanks
This regular expression will capture those sections:
\$\([^)]+\)
Then replace like this (this example changes each match to it's uppercase equivalent - you can add whatever custom logic you wish):
Regex.Replace(candidate, #"\$\([^)]+\)", delegate(Match m) {
return m.ToString().ToUpper();
});
I am not so good with delegate.s Here is what i came up with using Andrew's regex:
string test1 = #"$(gateway.jms.jndi.ic.url,0,tibjmsnaming, tcp)/topic/$(gateway.destination.prefix)$(gateway.StatusTopicName),$(gateway.jms.jndi.ic.username),$(gateway.jms.jndi.ic.password),abinding,tBinding";
string regex1 = #"\$\([^)]+\)";
var matches = Regex.Matches(test1, regex1);
Console.WriteLine(matches.Count);
foreach (Match match in matches)
{
test1 = test1.Replace(match.Value, "your String");
}
Console.WriteLine(test1);

Categories

Resources