Regex match xml attribute value [closed] - c#

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I want to get the integer value of this xml attribute limit=\"25\"
I tried this :Match match = Regex.Match(response.Content, "(?<=limit=))\\d+");
gives me an error : "too many )'s.
and this : Match match = Regex.Match(response.Content, #"limit=([0-9])$"
this returns nothing, the match is not successful
From this xml:
<issues type="array" limit="25" total_count="251" offset="0">
<issue>
<id>4317</id>

Your first regex has too many )s in it. Count them.
Your second is failing because of the quotation marks around the attribute value. Try "limit=\"([0-9])\"$" instead.
Lots of people will tell you to use an XML parser instead. I would strongly recommend that if you're doing anything more than very minor extraction of data from well-known XML, because XML itself isn't parseable with regular expressions.

Regex can be used for parsing XML since it is strict with its format but it is not recommended to use it
Use LINQ2XML
XElement doc=XElement.Parse(response.Content);
var value=doc.Attribute("limit").Value;
OR
var value=Regex.Match(response.Content, #"limit=""(\d+)""",RegexOptions.Singleline).Groups[1].Value;

It's better to use
string TheStringNeededToBeParsed = "... the xml ";
TheStringNeededToBeParsed .IndexOf(...

Related

What are the library that use C# 4.0 dynamic as an essential part of it? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
What are the library that use C# 4.0 dynamic as an essential part of it?
The most powerful uses of dynamic are evil incarnate and should never be used near production code.
For example:
var sql = SqlBuilder.MyDatabase
* "SELECT * FROM MyTable WHERE UserName = " + userName;
User result = sql;
dynamic could be used to turn userName into a parameter and turn the last line into an ExecuteReader() call.
Inspired by Jon Skeet
Another example:
var _ = RestClient.Builder;
var endpoint = _.https/_.api.stackexchange.com/2.1/_.answers
var results = endpoint(sort: "activity", order: "desc", site: "stackoverflow");
// Returns https://api.stackexchange.com/2.1/answers?order=desc&sort=activity&site=stackoverflow as dynamic JSON
The _ is needed to allow arbitrary identifiers.
You could get rid of the _ using expression trees:
var endpoint = RestClient.Build((https, api, answers) =>
https/api.stackexchange.com/2.1/answers
);
This uses parameter names to allow arbitrary identifiers.

C# remove text after comma [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
How can I remove specific text from a string?
for example I have this string:
string file = "43 , 2000-12-12 003203";
I need to remove the text after the comma, including comma, so I can have as a final result:
string file = "43";
thank you,
string file = "43 , 2000-12-12 003203";
string number = file.Split(',')[0].Trim();
You can do this:
string output = file.Substring(0, file.IndexOf(',')).Trim();
However, that might fail if the string doesn't contain a comma. To be safer:
int index = file.IndexOf(',');
string output = index > 0 ? file.Substring(0, index).Trim() : file;
You can also use Split as others have suggested, but this overload would provide better performance, since it stops evaluating the string after the first comma is found:
string output = file.Split(new[] { ',' }, 2)[0].Trim();
Possibly by using Split?
file.Split(',')[0].Trim();

Coding standard for calling multiple methods on same object in Java/ C# [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Could this code snippet
StringBuilder builder = new StringBuilder();
builder.Append("Have a ");
builder.Append("nice day!");
be better written like this?
StringBuilder builder = new StringBuilder();
builder.Append("Have a ")
.Append("nice day!");
In C# and Java, what would a better way to write it be?
The pattern in the 2nd example is a "fluent API" / "fluent interface"; frankly neither is strictly better - but if the second exists it can be slightly inefficient not to use it. In .NET IL terms, the first syntax involves an extra ldloc and pop per iteration, which the second avoids - but that is rarely hugely significant. Either will work fine.
Personally, I would optimize for readability and convenience unless you know it is in a performance-critical spot.
Indeed, you could just use:
string s = "Have a " + "nice day!";
which the compiler (in C#, at least - I don't know about java) will compute at compile-time (since they are both constants) and compile down to a single ldstr (which is automatically interned, too).

Regular Expression to check doubles, triples etc [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I need regular expression to retrieve no of doubles, triplets, tetras etc from a telephone number
following is the example,
number is 1001055522
it should return me
group Numbers
=============================
Doubles 00
22
Triplets 555
This regex when used with Regex.Matches will produce exact double or triple (not part of longer consecutive sequence). This is due to the greediness of the quantifier.
(\d)\1+
Demo
Well, the rest is to check the length of the string and count... I will leave it to you.
To find doubles, use a backreference:
(.)\1
Here's a demo: http://regex101.com/r/zC3fM1
To find triplets, just repeat the backreference:
(.)\1{2}
Here's a demo: http://regex101.com/r/cJ4lJ8
If you want to match all consecutive numbers regardless of how many there are, then use + on the backreference:
(.)\1+
Here's a demo: http://regex101.com/r/pL8sB3
Dim n = "1001055522"
Dim doubles = System.Text.RegularExpressions.Regex.Matches(n, "(.)\1")
Dim triples = System.Text.RegularExpressions.Regex.Matches(n, "(.)\1{2}")
'Doubles
For Each d As System.Text.RegularExpressions.Match In doubles
Console.WriteLine(d.Value)
Next
'Triples
For Each t As System.Text.RegularExpressions.Match In triples
Console.WriteLine(t.Value)
Next

Is there a Regular Expression that will never match any string? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Sort of a two part question:
Is there any theoretical regular expression that will never match any string (using general syntax without any fancy stuff provided by modern regular expression matchers)?
Is there a simple way to use C#'s Regex syntax to create a regex that will never match any string (this time, all the fancy stuff is included)?
NOTE: I am not referring to matching the empty string (that would be easy, just "").
Without multi-line mode, the end doesn't usually tend to appear before the beginning:
$.^
Or more simply, again without multi-line mode:
$.
With lookarounds, you can do all kinds of contradictory stuff:
(?=a)(?=b)
This forces a character to be two different things at once, which is of course impossible.
You could use contradictory lookbehinds, for example
\w(?<!\w)
Here \w will match any word character and the lookbehind (?<!\w) will make sure that the last character was not a word.
Just as you can match any characters with [\s\S], you can match no characters with [^\s\S] (or [^\w\W], etc).

Categories