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")
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 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\((.*?),(.*?)\)
I got strings which i want to take only those of them which have ending on for instance:
-id-3 (e.g somother33-id-3)
-id-203 (e.g som78estringetc-id-203)
-id-54 (e.g fwefwefwefw-id-3)
but sometimes i am retreiving strings which looks like this one i dont want to get
som78estringetc-id-203:someotherstring3-1
i am intrested only those string which ends by -id-somedigit
So string which i would like to get are those ending by convention:
somestring-id-digit
Could anyone help me how can i achieve that?
Using regex you can simply do:
-id-[0-9]*$
If you want to exclude your other item you could try:
[a-zA-Z0-9]*-id-[0-9]*$
try to use regex.
Regex.IsMatch("asdfadid-13234234", #"\w*-Id-[0-9]*$\b", RegexOptions.IgnoreCase)
this case best would be #"\w*-Id-[0-9]*$\b" right?
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.
It would be great if someone could provide me the Regular expression for the following string.
Sample 1: <div>abc</div><br>
Sample 2: <div>abc</div></div></div></div></div><br>
As you can see in the samples provided above, I need to match the string no matter how many number of </div> occurs. If there occurs any other string between </div> and <br>, say like this <div>abc</div></div></div>DEF</div></div><br> OR <div>abc</div></div></div></div></div>DEF<br>, then the Regex should not match.
Thanks in advance.
Try this:
<div>([^<]+)(?:<\/div>)*<br>
As seen on rubular
Notes:
This only works if there are not tags in the abc part (or anything that has a < symbol).
You might want to use start and end of string anchors (^<div>([^<]+)(?:<\/div>)*<br>$ if you want your string to match the pattern exactly.
If you want to allow the abc part to be empty, use * instead of +
That being said, you should be wary of using regex to parse HTML.
In this example, you can use regex because you are parsing a (hopefully) known, regular subset of HTML. But a more robust solution (ie: an [X]HTML parser like HtmlAgilityPack) is preferred when it comes to parsing HTML.
You need to use a real parser. Things like infinitely nested tags can't be handled via regex.
You could also include a named group in the the expression, e.g.:
<div>(?<text>[^<]*)(?:<\/div>)*<br>
Implemented in C#:
var regex = new Regex(#"<div>(?<text>[^<]*)(?:<\/div>)*<br>");
Func<Match, string> getGroupText = m => (m.Success && m.Groups["text"] != null) ? m.Groups["text"].Value : null;
Func<string, string> getText = s => getGroupText(regex.Match(s));
Console.WriteLine(getText("<div>abc</div><br>"));
Console.WriteLine(getText("<div>123</div></div></div></div></div><br>"));
NullUserException's answer is good. Here are a couple of questions, and variations, depending on what you want.
Do you want to prevent anything from occurring before the open div tag? If so, keep the ^ at the beginning of the regex. If not, drop it.
The rest of this post refers to the following section of the regex:
([^<]+?)
Do you want to capture the contents of the div, or just know that it matches your form? To capture, leave it as is. If you don't need to capture, drop the parentheses from the above.
Do you want to match if there is nothing inside the div? If so change the + in the above to *
Finally, although it will work fine, you don't need the ? in the above.
I think, this regex is more flexible:
<div\b[^><]*+>(?>.*?</div>)(?:\s*+</div>)*+\s*+<br(?:\s*+/)?>
I don't include the ^ and $ in the beginning and the end of my regex because we cannot assure that your sample will always in a single line.