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\((.*?),(.*?)\)
Related
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
I have a collection of url's and i need to write regular expression to filter needed content.
/data/43492-someText/"
/data/221639-anotherText/"
/data/116345-differentText/"
/data/6630-boooring/"
/data/220742-foo/"
What i need is only strings without /" on the end, so
/data/220742-foo
My Regular Expression looks like this:
#"/data/[0-9]{1,10}-.*""\s"
Note: I dont want to do this with string replace, because of some limitations on my project.
If that (string not ending in /) is the only requirement, then use something like this:
var desiredUrls = urls.Where(url => !url.EndsWith("/\""))
I initially read the question as a desire to filter urls but I can see how it could be a mapping question.
var withoutSuffix = urls.Select(url => url.TrimEnd("/\"".ToCharArray());
I think Regular Expressions are kind of overkill for what you're trying to do.
Anyways you can use something like this:
#"/data/[0-9]{1,10}-[^/]+"
You could use TrimEnd to remove the characters from the end of a string:
s.TrimEnd('/', '"')
You could use something like:
(/data/[0-9]{1,10}-.+)/
And the string without the trailing / will be in the first capture group.
I have been using this regular expression to extract file names out of file path strings:
Regex r = new Regex(#"\w+[.]\w+$+");
This works, as long as there is no space in the file name. For example:
r.Match("c:\somestuff\myfile.doc").Value = "myfile.doc"
r.Match("c:\somestuff\my file.doc").Value = "file.doc"
I need my regular expression to give me "my file.doc", and not just "file.doc"
I tried messing around with the expression myself. In particular I tried adding \s+ after learning that that is for matching whitespaces. I didn't get the results I hoped for.
I did devise a solution just to get the job done: I started at the end of the string, went backwards until a backslash was reached. This gave me the file name in reverse order (i.e. cod.elifym) into an array of chars, then I used Array.Reverse() to turn it around. However I'd like to learn how to achieve this by simply modifying my original regular expression.
Does it have to be a regular expression? Use System.IO.Path.GetFileName() instead.
Regex r = new Regex(#"[\w ]+\.\w+$");
A working regex might simply look like:
[^\\]+$
Consider using:
System.IO.Path.GetFileName(path)
I am new to regular expressions and the one that i have written might be a very simple one but donot know where I am wrong.
#"^([a-zA-Z._]+)#([\d]+)"
This RE is for the following string:
somename#somenumber
Now i am trying to retrieve the somename and somenumber. This is what i did:
ac.name = m.Groups[0].Value;
ac.number = m.Groups[1].Value;
Here ac.name reads the complete string, and ac.number reads somenumber. Where am I wrong in ac.name?
i guess the regex is correct, the problem is, you get the ac.name not from group 1 but group(0), which is the whole string. try this:
ac.name = m.Groups[1].Value;
ac.number = m.Groups[2].Value;
This regex is correct. I think your mistake is in somewhere else. You seem to use C#. So, you should think about the regex usage in the language.
Looking to the code sample in MSDN, you need to use 1-based indexes while accessing Groups instead of zero-based (as also Kent suggested). So, use this:
String name = m.Groups[1].Value;
String number = m.Groups[2].Value;
use this regex (\w+)#(\d+([.,]\d+)?)
Groups[1] will be contain name
Groups[2] will be contain number
I think you should move the + into the capture group:
#"^([a-zA-Z._]+)#([\d]+)"
If this is C#, try without the ^
([a-zA-Z\._]+)#([\d]+)
I just tried it out and it groups properly
Update: escaped the .
If you want only one match (and hence the ^ in original expression), use .Match instead of .Matches method. See MSDN documentation on Regular Expression Classes.
I wish to known if exist a clean way to split a string using different tags for opening and ending.
For example:
<&field1&>outside<&field2&>
using the function split:
string[] dd={"<&","&>"};
string[] b1 = a1.Split(dd,StringSplitOptions.None);
it show me:
0:
1:field1
2:outside
3:field2
4:
(that it is that i want to do).
but also
<&field1<&outside<&field2<&
show the same.
#"\G<&(?<code>.*?)&>"
The TemplateParser in the AspCodeRegex class in System.Web.RegularExpressions uses something similar to this(answer via #rexm)
You should use a regular expression to do this. After a quick play I came up with this which seems to match the entries within the <& &> delimiters, but you get the idea:
<&([^&]*)&>
See Regular Expression Examples for some more examples and also the code you need to run your regex.