I am trying to use variable inside the below regex but I am unable to get the correct output.
(?<=="Page 2" class="fl" href=")(.*?)(?=\"><span class=\"csb)
--
I tried below but not sure I missed something.
(("?<== \"Page "+ pages++ + "\" class=\"fl\" href=\"")"(.*?)"(?=\"><span class=\"csb"));
--
How I can achieve it?
Edit:
aria-label="Page 2" class="fl" href="/search?q=.net+regex+tester&ei=p3Q7XNzkBt6RwgPrga-YBQ&start=10&sa=N&ved=0ahUKEwic1OvFo-vfAhXeiHAKHevAC1MQ8tMDCHw"><span class="csb ch"
--
Added the source.
We need to view on this issue not like "how to write correct regex query".
Just look like whole c# regex variable (object).
Your code in c# looks like (added double quotes)
var reg = new Regex(#"(?<==""Page 2"" class=""fl"" href="")(.*?)(?=\""><span class=\""csb)");
So, we need to String.Format the Regex string query, like
var reg = new Regex($#"(?<==""Page {page}"" class=""fl"" href="")(.*?)(?=\""><span class=\""csb)");
Btw, if we are talking about parsing html, it's strongly not recommended to use Regular Expression. More info
Related
Hi all I need to extract Guid from the following string
<PageFieldFieldValue:FieldValue FieldName='fa564e0f-0c70-4ab9-b863-0177e6ddd247' runat='server'></PageFieldFieldValue:FieldValue>
<PageFieldRichImageField:RichImageField FieldName="3de94b06-4120-41a5-b907-88773e493458" runat="server"></PageFieldRichImageField:RichImageField>
What i need is to get is "fa564e0f-0c70-4ab9-b863-0177e6ddd247" and "3de94b06-4120-41a5-b907-88773e493458" in this case, However this guid is dynamic and will change every time and there are lot more guids in the string that i have and I need to get all those guids so that I can add them to a colection.
Note: The string is actually an aspx page content. All nodes are different but have same property "FieldName" which I need to get.
I went through the link C# RegEx string extraction and construcked the regex in same way. Here is what I did :
string s = #"<PageFieldFieldValue:FieldValue FieldName='fa564e0f-0c70-4ab9-b863-0177e6ddd247' runat='server'>
</PageFieldFieldValue:FieldValue>";
Regex reg = new Regex(#"FieldName=(?<ReferenceId>{36})");
Match match = reg.Match(s);
string guid = match.Groups["ReferenceId"].Value;
How ever this didnt work for me. I get exception"parsing "FieldName=(?{35})" - Quantifier {x,y} following nothing." while creating the Regex object "reg".
If i dont use {36} which is suppose to be the length of GUiD:
Regex reg = new Regex(#"FieldName=(?<ReferenceId>)")
I dont get any exception but I dnt get desired result either. match.Groups["ReferenceId"].Value returns empty string
Try using sth. like that:
(?<=FieldName=['"])[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}(?=['"])
Explanation
(?<=FieldName=['"]) prepended by FieldName= and " or '
[a-f\d]{8}-[a-f\d][...] followed by GUID (which is what is actually matching)
(?=['"]) followed by " or '
See this in action at Regex101
The issue you are having is basically that you are providing the quantifier {36} but are not telling it what to quantify - you need some character matching expression right before the quantifier. For, example I just added the '.' before {36} in your example (meaning "match any 36 characters") and it seems to work. Oh, and I also added the missing apostrophe after "FieldName=":
Regex reg = new Regex(#"FieldName='(?<ReferenceId>.{36})");
Working example: https://regex101.com/r/1tbien/1
This is to my deleted question here:
https://stackoverflow.com/questions/20843964/need-regex-to-find-text-in-c-sharp
I have a string similar to this:
<Label Content="Hi"/>
<SomeControl Header ="welcome"/>
From the above string data, I want to get:
Content="Hi"
Header="welcome"
The regex expression can be combined or separate to get these two strings.
Before I edit the question, it got deleted. I wanted to make following edit:
By trying online regex testers, I managed to try following:
Content="[^"]*")
But when I put the same in C# string, I get error:
string expr = #"Content="[^"]*")";
I know that some string escape sequence needed. So wanted to find it. However I am not able to find it yet. Why I want to file such xml (XAML file) like this is I want to find number of occurrences of hard coded string. So I do not need any xml parsing or anything like that. Just plain simple regex to found count of such strings.
I understand your point.
You can definitely capture multiple results with a RegEx, when you're sure other ways aren't appropriate. (although xml is a pretty handy format)
Did you possibly mean to use .Match*es* at the end, though?
string expr = #"Content=""[^""]*""";
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(expr);
string data = #"<SomeControl Content=""sup""><anotherControl Content=""hey""><athird Content=""yo""></athird></anotherControl></SomeControl>"; // This will be replaced with actual file content
var res = reg.Matches(data);
var occuranceCount = res.Count;
Finally following worked for me:
string expr = #"Content=""[^""]*""";
Regex reg = new Regex(expr);
string data = #"<SomeControl Content=""Hi"">"; // This will be replaced with actual file content
var res = reg.Match(data);
var occuranceCount = res.Groups.Count;
I am using HighCharts and am generating script from C# and there's an unfortunate thing where they use inline functions for formatters and events. Unfortunately, I can't output JSON like that from any serializer I know of. In other words, they want something like this:
"labels":{"formatter": function() { return Highcharts.numberFormat(this.value, 0); }}
And with my serializers available to me, I can only get here:
"labels":{"formatter":"function() { return Highcharts.numberFormat(this.value, 0); }"}
These are used for click events as well as formatters, and I absolutely need them.
So I'm thinking regex, but it's been years and years and also I was never a regex wizard.
What kind of Regex replace can I use on the final serialized string to replace any quoted value that starts with function() with the unquoted version of itself? Also, the function itself may have " in it, in which case the quoted string might have \" in it, which would need to also be replaced back down to ".
I'm assuming I can use a variant of the first answer here:
Finding quoted strings with escaped quotes in C# using a regular expression
but I can't seem to make it happen. Please help me for the love of god.
I've put more sweat into this, and I've come up with
serialized = Regex.Replace(serialized, #"""function\(\)[^""\\]*(?:\\.[^""\\]*)*""", "function()$1");
However, my end result is always:
formatter:function()$1
This tells me I'm matching the proper stuff, but my capture isn't working right. Now I feel like I'm probably being an idiot with some C# specific regex situation.
Update: Yes, I was being an idiot. I didn't have a capture around what I really wanted.
`enter code here` serialized = Regex.Replace(serialized, #"""function\(\)([^""\\]*(?:\\.[^""\\]*)*)""", "function()$1");
that gets my match, but in a case like this:
"formatter":"function() { alert(\"hi!\"); return Highcharts.numberFormat(this.value, 0); }"
it returns:
"formatter":function() { alert(\"hi!\"); return Highcharts.numberFormat(this.value, 0); }
and I need to get those nasty backslashes out of there. Now I think I'm truly stuck.
Regexp for match
"function\(\) (?<code>.*)"
Replace expression
function() ${code}
Try this : http://regexr.com?30jpf
What it does :
Finds double quotes JUST before a function declaration and immediately after it.
Regex :
(")(?=function()).+(?<=\})(")
Replace groups 1 & 3 with nothing :
3 capturing groups:
group 1: (")
group 2: ()
group 3: (")
string serialized = JsonSerializer.Serialize(chartDefinition);
serialized = Regex.Replace(serialized, #"""function\(\)([^""\\]*(?:\\.[^""\\]*)*)""", "function()$1").Replace("\\\"", "\"");
I'd like to replace a line in a text file using a c# function in asp.net. The line is:
SQL-SERVER-VERSION="some unknown value"
I don't know what the value after = might be so I need to use a wildcard for this. I want the new line to read:
SQL-SERVER-VERSION="2008"
I'm trying to use Regex.Replace but no matter what regular expression I try, it doesn't work.
Can anybody help?
Thanks,
John
I don't know what you already tried so I can't tell you what you were doing wrong, but the following should work:
string s = "SQL-SERVER-VERSION=\"some unknown value\"";
s = Regex.Replace(s, "SQL\\-SERVER\\-VERSION=\".*\"", "SQL-SERVER-VERSION=\"2008\"");
Try this:
Regex rgx = new Regex(#"SQL-SERVER-VERSION="".*?""");
string result = rgx.Replace(input, replacement);
It looks a little messy in a .NET string but the pure regex looks like this:
SQL-SERVER-VERSION=".*?"
If you know that " won't appear in the value then you could locate the string using
SQL-SERVER-VERSION=".*"
and replace with SQL-SERVER-VERSION="2008"
e.g
strInput = Regex.Replace( strInput, "SQL-SERVER-VERSION="".*""", "SQL-SERVER-VERSION=""2008""")
So, Im trying to make a program to rename some files. For the most part, I want them to look like this,
[Testing]StupidName - 2[720p].mkv
But, I would like to be able to change the format, if so desired. If I use MatchEvaluators, you would have to recompile every time. Thats why I don't want to use the MatchEvaluator.
The problem I have is that I don't know how, or if its possible, to tell Replace that if a group was found, include this string. The only syntax for this I have ever seen was something like (?<group>:data), but I can't get this to work. Well if anyone has an idea, im all for it.
EDIT:
Current Capture Regexes =
^(\[(?<FanSub>[^\]\)\}]+)\])?[. _]*(?<SeriesTitle>[\w. ]*?)[. _]*\-[. _]*(?<EpisodeNumber>\d+)[. _]*(\-[. _]*(?<EpisodeName>[\w. ]*?)[. _]*)?([\[\(\{](?<MiscInfo>[^\]\)\}]*)[\]\)\}][. _]*)*[\w. ]*(?<Extension>\.[a-zA-Z]+)$
^(?<SeriesTitle>[\w. ]*?)[. _]*[Ss](?<SeasonNumber>\d+)[Ee](?<EpisodeNumber>\d+).*?(?<Extension>\.[a-zA-Z]+)$
^(?<SeriesTitle>[\w. ]*?)[. _]*(?<SeasonNumber>\d)(?<EpisodeNumber>\d{2}).*?(?<Extension>\.[a-zA-Z]+)$
Current Replace Regex = [${FanSub}]${SeriesTitle} - ${EpisodeNumber} [${MiscInfo}]${Extension}
Using Regex.Replace, the file TestFile 101.mkv, I get []TestFile - 1[].mkv. What I want to do is make it so that [] is only included if the group FanSub or MiscInfo was found.
I can solve this with a MatchEvaluator because I actually get to compile a function. But this would not be a easy solution for users of the program. The only other idea I have to solve this is to actually make my own Regex.Replace function that accepts special syntax.
It sounds like you want to be able to specify an arbitrary format dynamically rather than hard-code it into your code.
Perhaps one solution is to break your filename parts into specific groups then pass in a replacement pattern that takes advantage of those group names. This would give you the ability to pass in different replacement patterns which return the desired filename structure using the Regex.Replace method.
Since you didn't explain the categories of your filename I came up with some random groups to demonstrate. Here's a quick example:
string input = "Testing StupidName Number2 720p.mkv";
string pattern = #"^(?<Category>\w+)\s+(?<Name>.+?)\s+Number(?<Number>\d+)\s+(?<Resolution>\d+p)(?<Extension>\.mkv)$";
string[] replacePatterns =
{
"[${Category}]${Name} - ${Number}[${Resolution}]${Extension}",
"${Category} - ${Name} - ${Number} - ${Resolution}${Extension}",
"(${Number}) - [${Resolution}] ${Name} [${Category}]${Extension}"
};
foreach (string replacePattern in replacePatterns)
{
Console.WriteLine(Regex.Replace(input, pattern, replacePattern));
}
As shown in the sample, named groups in the pattern, specified as (?<Name>pattern), are referred to in the replacement pattern by ${Name}.
With this approach you would need to know the group names beforehand and pass these in to rearrange the pattern as needed.