I'm trying to extract a url from a string.
{ns:"images",k:"5127",mid:"A04F21EB77CF61E10E43BA33CF1986CA44357448"
,md5:"e2987d19c953bd836ec8fd2e0aa8492",surl:"http://someURLIdontwant/"
,imgurl:"http://THISISTHEURLINEED.jpg",tid:"OIP.Me2987d199c953bd836ec8fd2e0aa8492H0"
,ow:"300", docid:"608010036892077154",oh:"225",tft:"49"}
So it is located after "imgurl:". I am no expert on Regex and all I could produce is:
imgurl:'(.*)',tid
whitch worked on some online regex tester. But not the way I'm using it in C# apperantly.
webClient.DownloadFile(System.Text.RegularExpressions.Regex.Match
(stringWithText, "imgurl:'(.*)',tid").Groups[1].Value,"path\file.jpg");
Can it be done? Thanks
As #WiktorStribiżew already pointed out: The expression is almost correct. Use this instead:
Regex.Match(stringWithText, "imgurl:\"(.*)\",tid").Groups[1].Value
Example on dotNetFiddle
And as I mentioned earlier in a comment: You should parse the Json data instead.
Related
I'm trying to write a parser that will create links found in posted text that are formatted like so:
[Site Description](http://www.stackoverflow.com)
to be rendered as a standard HTML link like this:
Site Description
So far what I have is the expression listed below and will work on the example above, but if will not work if the URL has anything after the ".com". Obviously there is no single regex expression that will find every URL but would like to be able to match as many as I can.
(\[)([A-Za-z0-9 -_]*)(\])(\()((http|https|ftp)\://[A-Za-z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?)(\))
Any help would be greatly appreciated. Thanks.
Darn. It seems #Jerry and #MikeH beat me to it. My answer is best, however, as the link tags are all uppercase ;)
Find what: \[([^]]+)\]\(([^)]+)\)
Replace with: $1
http://regex101.com/r/cY7lF0
Well, you could try negated classes so you don't have to worry about the parsing of the url itself?
\[([^]]+)\]\(([^)]+)\)
And replace with:
$1
regex101 demo
Or maybe use only the beginning parts to identify a url?
\[([^]]+)\]\(((?:https?|ftp)://[^)]+)\)
The replace is the same.
Is it possible to use a Regular Expression to extract only comments from a C# file?
If so how would you do that?
Refer this: -> Finding Comments in Source Code Using Regular Expressions
after reading the article, your final RegEx would be.
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)
FOR C#
^/[/|*](.+)$ (for single line comment )
(^\/\/.*?$|\/\*.*?\*\/) (for multilne comments)
This Regex finds all the comments in a C# file.
(((?<=//).+(?=\n))|((?<=/*)[^(\*/)]+(?=\*/)))+
We have implemented the invocation of brill tagger from our c# code. We just neede to know what is the correct Regex regular expression for eliminating all from a string, but jst keep a-z,A-Z, full stop and comma. We tried [^a-zA-Z\.\,] on the online regular expression tester and it is giving the correct result, but when implemented in C#, it is not working properly. We also tried several other combinations but we are not getting the correct result.
This is the format in which we are writing:
strFileContent = Regex.Replace(strFileContent, #"[^a-zA-Z\.\,]", "");
but we are not getting the desired output. what is wrong??
Regex.Replace(yourString, #"[^a-z\.\,]", string.Empty, RegexOptions.IgnoreCase)
EDIT: I can't see anything wrong with what you are doing, my answer is exactly the same. I tested both in LINQPad and they both return the same result.
I've got a string split, using a regular expression.
Regex.Split(str, #"\s");
What does this convert to without regex? reason being I'm porting this to PHP and an SQL function. Unless you can show me the PHP code for the same...
You can use preg_split
preg_split("/\\s/", str);
The theme is i opened a file and get all it's data into string and i am matching this string with the regex returning none. But the same regex in PHP is returning values for the same text using preg_match_all. Anyone having a idea?
The method in .NET that’s closest to preg_match_all() is the static Regex.Matches(String,String) call, or the equivalent Matches method on a compiled regular expression. It returns a MatchCollection that you can use to count the matches and to loop over each one.
Can you provide some short, self-contained code to show what’s not working?
There is a Regex.Matches method in C# that you can use.