Can this be done without a regular expression? - c#

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);

Related

Using regular expression on string for use in C#

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.

Retrieve array from string with regular expression in C#

I have following string:
"option1,option2->data1,data2,data3,..."
I'm learning C# and also regular expressions, so I thought I might have some fun with it, but I can't figure out how to get an array from this.
For example, I'd like to retrieve array of strings that looks like this one:
[option1,option2,data1,data2,data3,...]
Here's regular expression I wrote in regex tester (.+),(.+)->((.+),?), but I'm not sure if this will work. And also I don't know how to use regex functions in C# to achieve this. I guess I should use something from System.Text.RegularExpressions but I'm not really sure what.
Long story short:
I want to get array from string using regular expressions.
"option1,option2->data1,data2,..." -> [option1,option2,data1,data2,...]
Thanks!
I would avoid RegularExpressions for this. you can simply do this:
string[] myArray = inputString.Replace("->", ",").Split(',');
It seems like you need direction more than an answer.
http://www.RegexHero.com is a good place to test your regex against strings.
http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet is a cheat sheet/guide for c# regex
http://www.dotnetperls.com/regex-match here's a good place to start with regex in c#
You haven't posed anything that string.Split can't handle:
var split = given.Split(new [] {",", "->"}, StringSplitOptions.None);

Equivalent of Substring as a RegularExpression

Ok, I need a general regular expression that will give me the x characters from a string starting at position y like the string's substring function:
input_str.Substring(y,x)
But as a C# regular expression.
Example:
1234567890 Substring(5,3) 678
I know you are thinking why not just use the Substring function? The short answer is because this goes as a data for an existing function and in this context it would be inelegant to create a whole separate data parsing mechanism. We'd like to get this working without changing the code.
I feel like this is really obvious--but I'm pretty inexperienced with regular expressions. Thanks in advance for any help.
.{y}(.{x}).* should do it, I think, then just pull out the capture group.

Regex Regular expression in c#

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.

What is C# equivalent of preg_match_all?

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.

Categories