Regular Expression in C# (Regex) [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've got the following input text:
10,"ABASTECEDORA NAVAL Y INDUSTRIAL, S.A.",-0- ,"CUBA"
I need a String[] result with
result[0] == "10"
result[1] == "ABASTECEDORA NAVAL Y INDUSTRIAL, S.A."
result[2] == "-0-"
result[3] == "CUBA"
Please help to give me a regex pattern to split the input for above result.

It looks like you are reading a CSV file with optional quotations and you want to parse a single line. Take a look at this excellent .NET CSV reader API:
http://www.codeproject.com/KB/database/CsvReader.aspx

It appears that this may be a CSV file that you are reading in, if so you should use the .NET CSV api.
However if you really want to use regex, you can use the Regex.split() function to split your input into a String[].
http://msdn.microsoft.com/en-us/library/8yttk7sy.aspx

Something like this will work with your specific example:
(\d+),"([^"]+)",([^,]+),"([^"]+)"
However, it looks like you're really parsing CSV, so I'd use an appropriate CSV library for this. The pattern I provided won't account for embedded and escaped quotations / commas within the String, etc.

Related

How To remove the http://www from the any given url in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I need help to remove
http://www. or www. from the any URL. For example
http://www.stackoverflow.com/questions/ask
http://stackoverflow.com/questions/ask
www.stackoverflow.com/questions/ask
for the above all I need stackoverflow.com/questions/ask
This is really really simple
var url = "http://www.stackoverflow.com/questions/ask";
url = url.Replace("http://","").Replace("www.","")
Try This:
string strNew= str.Replace("http://","").Replace("www.","");
Try converting the Url to String, then using the Replace() method on it to replace a part of it with some other text or a empty string. Here would be the example:
var url = Request.Url.ToString();
/* if you want to get the URL first, then try the above code to get URL
* otherwise, just use a simple string object */
url.Replace("http://www.", "").Replace("www.", "");
Now when you'll execute the code, you'll get the URL without the http:// or www. part.
Also, note the double usage of Replace() method. That would replace them both.

Regular Expression - One Item Has Multiple Words [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have multiple lines in a text file.
The text file looks similar to this:
Column 1 Column 2 Column 3
12345 stack overflow 12345678
I need a regular expression to check this and then grab column two. My problem is column two can be one or multiple words and I need it to be one item in the string when I grab it or grab other columns.
Read the file line by line and match it using the following regex:
^\d*\s*([\w\s]*\w)\s*\d*$
Now, the 1st named subgroup should give you what you need. I'm not exactly sure about C# syntax, but for notepad++, $1 works well.
The ^ ensures that the regex starts matching from the very beginning of the read line and the $ ensures that it matches up till the very end.
The default greedy matching of the regex assures that no extra spaces are captured in the beginning of the column two content and the \w at the end ensures no trailing spaces.
If the carriage return and new-line characters are read by your platform as well, you can modify it as:
^\s*\d*\s*([\w\s]*\w)\s*\d*\s*$

Regex for Currency with ',' and/or '.' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm kinda rusty with regular expressions. I need a REGEX that will validate values formatted like the following:
123.00
123,00
1324,00
1234.00
123
1213.0
I tried ^\d.\d{2}$, but it does not seem to match all values.
Appreciate any assistance.
You can use the following:
\d+[.,]?\d+
Good luck!
\d+[,.]?\d*
I would strongly advise against mixing cultures especially for persistence or transport.
The Regex you're likely looking for is something like #"\d+([,.]\d+)?"
It specifies "Some number of digits, optionally followed by a . or , and at least one digit". It would not match 123..
If you want to match culture-specific strings, however, I'd recommend using NumberFormatInfo.CurrencyDecimalSeparator and then look for that specifically.

How to extract the string in the quotes (either double quotes or single quotes) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Question 1:
For example,
... "ab'cd" ... 'ab"cd' ...
It should get ab'cd and ab"cd respectively. There may be many quoted strings.
Question 2:
And how to extract text <td>...</td> text from the following string?
<abc>text <td>...</td> text</abc>
<xyz>text <td>...</td> text</xyz>
<def>text <td>...</td> text</def>
abc, def, xyz may be nested in <td>.
regex:
(?:"(?<text>[^"]*)")|(?:'(?<text>[^']*)')
and the whole snippet:
Regex regex = new Regex(
#"(?:""(?<text>[^""]*)"")|(?:'(?<text>[^']*)')",
RegexOptions.None
);
Testable on my blog (requires silverlight)
Looks like this is a 2-in-1 question right?
My answer to 1 is
((.*)['"](.*))
then for #2
<(abc|xyz|def)>(.*)<\/(abc|xyz|def)>
abc | xyz | def <- imagine it like this
dont forget to TRIM the spaces before you use the results
for first question use:
("(?<content>[^"]*)")|('(?<content>[^']*)')
for second question:
>(?<content>\w*\s*<td>.*</td>[\w\s]*)<
and get group named content for both.
I figured it out - using back reference.
(["'])(?<q>.+?)\1

Custom regular expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi I need to match this format
N - Number
NN,NN
or
NN.NN
also
N,N and N.N
and combinations
N.NN and N,NN or NN,N and NN.N
Here's your regex:
\d{1,2}[.,]\d{1,2}
See it here in action: http://regexr.com?2vman
Here's a slightly different version:
\d\d?[.,]\d\d?
See it here in action: http://regexr.com?2vmaq
If you want to also match with out a decimal, use this:
\d\d?[.,]?\d{0,2}
See it here in action: http://regexr.com?2vml4
How about:
\d{1,2}(?:[.,]\d{1,2})?
explanation:
\d{1,2} : one or two digits
(?: : start non capture group
[.,] : . or ,
\d{1,2} : one or two digits
)? : end group, optional
Why match it?
Just remove the commas and use the actual number:
Regex.Replace("8,675,309.02", "(,)", string.Empty) // Outputs 8675309.02
If this is a validation scenario, using int.Parse will let you know if its valid.
I would go with something like this:
Regex regex = new Regex(#"\d{1,2}[\.,]\d{1,2}");

Categories

Resources