C# Regex Extract Method and Parameter Names - c#

I need help with regex, where I pass this kind of string:
"MethodName(int? Property1, string Property2, List<int?> Property3)"
and receive method and property names as string array. Something like this:
["MethodName","Property1","Property2","Property3"]
I've tried this:
Regex to get method parameter name
and this
Regex to extract function-name, & it's parameters
But could not get results I needed

You can achieve this using much simpler regex. Use this regex, which ensures it only matches method names or variable names by using look ahead to see what follows is optional space and either ( or , or )
\b\w+(?=\s*[,()])
Demo

You can do something like this:
^(\w+)\((((.*)(\s)(.*)),((.*)(\s)(.*)),((.*)(\s)(.*)))\)
Keep in mind you have multiple groups.
https://regex101.com/r/2LDf6X/1
It's up to you to find a method to simplify this regex to catch variables parameters not only three.
As suggested by the user below, this is the correct and simplier regex:
\b\w+(?=\s*[,()])
Here a demo: https://regex101.com/r/WrG2kF/1

Related

C# Regular Expression Reversing Match

I am looking to convert a part of a string which is substringof('has',verb) into contains(verb,'has')
As you can see, what is changing is just substring to contains and the two parameters passed to the function reversed.
I am looking for a generic solution, by using regex. Preferably using tags. i.e once i get two matches, i need to be able to reverse the matches by using $2$1 (This is how i remember doing this in perl)
You can use this regular expression code:
var re = new Regex(#"substringof\('([^']+)',([^)]+)\)");
string output = re.Replace(input, #"contains($2, '$1')");
.NET Fiddle example
You can use a regex like this:
.*?\((.*?),(.*?)\)
Working demo
Then you can use a string replacement like this:
contains(\2,\1) or
contains($2,$1)
Btw, if you just want to change the substringof, then you can use:
substringof\((.*?),(.*?)\)

How do you modify the matched string using Regex.Replace?

Say I want to convert these strings:
www.myexample.com and http://www.myexample.com
into:
<a href='http://www.myexample.com'>http://www.myexample.com</a>
using Regex.Replace
I've come up with this:
Regex.Replace(string, pattern, "$&")
My problem is that I don't know how to check if the matched string $& starts with http:// and adds it if necessary.
Any ideas?
If you don't have to consider https or things like that, you could maybe use this:
Regex.Replace(string, #"(?:http://)?(.+)", "http://$1")

Copy an Entire String from a Keyword

So, I want to do this,
For example, there is a string called [FULLNAME]-Awesome Guy-[END],
But there are multiple strings in a list, so like:
[OTHER]-AG-[END]
[FULLNAME]-Awesome Guy-[END]
[NICKNAME]-AG-[END]
My question is, how can I find [FULLNAME] then set a string as [FULLNAME]-Awesome Guy-[END]
Can you guys help?
Thanks!
i'd probably recommend using a regular expression here if you just need something quick. if you need something more robust and able to handle breaking up the various tags, you might want to look at writing up your own basic parser to break stuff up by tag and let you search that way.
this code:
string s = "[OTHER]-AG-[END] [FULLNAME]-Awesome Guy-[END] [NICKNAME]-AG-[END]";
Regex re = new Regex(#"\[FULLNAME\][^[]+\[END\]");
Console.WriteLine(re.Match(s));
prints
[FULLNAME]-Awesome Guy-[END]
although it will give you malformed results if there is a [ character in the name somewhere.

C# string masking/formatting/filtering with or without regex

Hopefully this isn't too complicated, I just can't seem to find the answer I need.
I have a string with variables in, such as: this is a %variable% string
The format of the variables within the string is arbitrary, although in this example we're using the filter %{0}%
I am wanting to match variable names to properties and ideally I don't want to loop through GetProperties, formatting and testing each name. What I'd like to do is obtain "variable" as a string and test that.
I already use RegEx to get a list of the variables in a string, using the given filter:
string regExSyntax = string.Format(syntax, #"(?<word>\w+)");
but this returns them WITH the '%' (e.g. '%variable%') and as I said, that filter is arbitrary so I can't just do a string.Replace.
This feels like it should be straight-forward....
Thanks!
"(?<word>\w+)"
Is just capturing anything alphnumeric and putting it into a named capturing group called "Word"
You might be interested in learning about lookbehind and lookahead. For example:
"(?<=%)(?<word>\w+)(?=%)"
You can make it a bit more generic with putting your filter in a seperate variable:
string Boundie = "%";
string Expression = #"(?<=" + Boundie + #")(?<word>\w+)(?=" + Boundie + #")";
I hope this is anywhere near what you are looking for.
Given that your regex syntax is: string regExSyntax = string.Format(syntax, #"(?<word>\w+)");, I assume you're then going to create a Regex and use it to match against some string:
Regex reExtractVars = new Regex(regExSyntax);
Match m = reExtractVars.Match(inputString);
while (m.Success)
{
// get the matched variable
string wholeVar = m.Value; // returns "%variable%"
// get just the "word"
string wordOnly = m.Groups["word"].Value; // returns "variable"
m = m.NextMatch();
}
Or have I completely misunderstood the problem?
Acron,
If you're going to roll-your own script parser... apart from being "a bit mad", unless that's the point of the exercise (is it?), then I strongly suggest that you KISS it... Keep It Simple Stoopid.
So what denotes a VARIABLE in your scripting syntax? Is it the percent signs? And they're fixed, yes? So %name% is a variable, but #comment# is NOT a variable... correct? The phrase "that filter is arbitrary" has me worried. What's a "filter"?
If this isn't homework then just use an existing scripting engine, with existing, well defined, well known syntax. Something like Jint, for example.
Cheers. Keith.

Does .NET Regex support global matching?

I haven't been able to find anything online regarding this. There's RegexOptions, but it doesn't have Global as one of its options. The inline modifiers list also doesn't mention global matching.
In a nutshell, I've got a regex to parse something like
--arga= "arg1" --argb ="arg2"
into separate argument name/value pairs using this regex:
--(\\w+)\\s*=\\s*\"(\\w+)\"\\s*
but the .NET Regex class doesn't do it globally (iteratively). So in order for me to get this to work, I'd have to do a match, then remove this from the argument string, and loop over and over again until I've exhausted all of the arguments.
It would be nicer to run the regex once, and then loop over the match groups to get the name value pairs. Is this possible? What am I missing?
You're looking for the Regex.Matches method (plural), which returns a collection containing all of the matches in the original string.

Categories

Resources