Dynamic guid extraction using Regex - c#

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

Related

Inserting variable inside regex

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

C# Regex URL Port username & password

I have a URL and need to extract the port, username and password from it and put them into an array. It looks like following.
http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts
Can I use some other method without replaces or substring?
One of the ways in C#
Get the query parameter
var parsedQuery = HttpUtility.ParseQueryString("http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts");
Then, below will give the username
parsedQuery["username"]
For Password:
parsedQuery["password"]
For port you can use URI :
Uri uri = new Uri("http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts");
Get the port by
uri.Port
Create an array or use whatever you require to club.
I don't know C#, but here's one that works for Python. It's pretty straightforward so you should be able to convert.
:(?P<port>[0-9]+).*username=(?P<username>[a-zA-Z0-9]+).*password=(?P<password>[a-zA-Z0-9]+)
The (?P<foo>bar) syntax is a named capture group that will put a variable matching the pattern 'bar' into a variable called 'foo' when you extract them.
Here is another possible solution with pure C# regex:
var url = "http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts";
var urlRegex = new Regex(#"(?<=(http(s)?://)?\w+(\.\w+)*:)\d+(?=/.*)?");
var usernameRegex = new Regex(#"(?<=(\?|&)username=).*?(?=&|$)", RegexOptions.IgnoreCase);
var passwordRegex = new Regex(#"(?<=(\?|&)password=).*?(?=&|$)", RegexOptions.IgnoreCase);
Console.WriteLine(urlRegex.Match(url));
Console.WriteLine(usernameRegex.Match(url));
Console.WriteLine(passwordRegex.Match(url));
If there are any parts that don't change, e.g. if it's always the same url you could just replace it like this
string str = "http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts"
str.Replace("http://myproject.ddns.net","");
This would leave you ":8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts"
There is nothing stopping you repeating the process with another section.
As for regex you could use Regex.Match https://msdn.microsoft.com/en-us/library/twcw2f1c(v=vs.110).aspx to get the parts you want.
You could use ":\d{4}/" to get the port - you'd have to strip the leading ":" and trailing "/" though; this "username=\w*\&" to get the username - you'd have to strip the leading "username=" and trailing "&" though; and for the password you could use "password=\w*\&" - you'd have to strip the leading "password=" and trailing "&" though.
If you'd like to experiment with regex this site https://regex101.com/ is pretty good.

How to get a part of a string (url)?

I have the following url
http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51
I need to extract the A5AF5C0D-648A-4892-A995-CDA8013F2643 portion of the url parameter:
Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643
This can be in the middle or at the end of the url. I cannot guarantee the position of it. But i always starts with Proj= and end with &. The string between this is what i want. How can i grab this within C#?
It seems that you are trying to retrieve the IDFA from a url address. I think you can easily do that by applying regular expressions to the url string.
For example, the following:
[0-9a-fA-F]{8}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][[0-9a-fA-F]{12}
Picks up every valid IDFA when applied to the URL string. You can add conditions for the head and tail of the IDFA to retrieve exactly what you are looking for:
Proj=[0-9a-fA-F]{8}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][[0-9a-fA-F]{12}&
You can test the above Regex (regular expression) syntax on one of the many free online Regex applets (e.g. https://regex101.com/)
To apply Regex to your code, please see the following thread:
c# regex matches example
You may need to create a Uri and pass the value of its Query property to the HttpUtility.ParseQueryString method:
string value = HttpUtility.ParseQueryString(new Uri("http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51").Query)["Proj"];
The method is defined in System.Web.dll by the way so you need to add a reference to this one.
string som = "http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51";
int startPos = som.LastIndexOf("Proj=") + "Proj=".Length + 1;
int length = som.IndexOf("&") - startPos;
string sub = som.Substring(startPos, length); //<- This will return your key
This should do it.
One solution:
string param = HttpUtility
.ParseQueryString("http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51")
.Get("Proj");

Need regex to find text in C#

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;

Alternative to RegEx

I am currently passing a parameter to a SQL string like this -
grid=0&
And I am using a RegEx to get the 0 value like so-
Match match = Regex.Match(input, #"grid=([A-Za-z0-9\-]+)\&$",
RegexOptions.IgnoreCase);
string grid = match.Groups[1].Value;
which works perfectly.
However as development has progressed it is clear that more parameters will be added to the string like so-
grid=0&hr=3&tb=0
These parameters may come in a different order in the string each time so clearly the RegEx I am currently using wont work. I have looked into it and think Split may be an option however not sure.
What would the best method be and how could I apply it to my current problem?
If you're parsing query string and looking for an alternative to Regex, there is a specialized class and method for that, it returns collection of parameters:
string s = "http://www.something.com?grid=0&hr=3&tb=0";
Uri uri = new Uri(s);
var result = HttpUtility.ParseQueryString(uri.Query);
You have to include System.Web namespace.
You can access each of the parameters' values by using it's key:
foreach (string key in result.Keys)
{
string value = result[key];
// action...
}
Regexes can still be used here. Consider adding another capture group to capture the property name, and then looping over all of the results using Matches rather that Match, or calling Match multiple times.

Categories