Regular Expression to check doubles, triples etc [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 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

Related

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

How can I insert a value with decimal places as 00 in SQL Server database? [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.
In this table I want to insert values in the weight column that is of float type with decimal
values 00 like 100.00, 150.00, 25.00 .
In this table if I am going to insert 25.5 then it will show decimal value bit if I am storing 135.00 it will store 135 only not showing decimal if 00.
I'm not sure from your question what you are actually looking for. Storage and Display are two separate things.
STORAGE
If you want to store a number with a fixed precision in SQL SERVER use DECIMAL
CREATE TABLE MyTable
(
MyNumber DECIMAL(5,2)
)
The above will store 5 digits, 3 before the decimal point, and two after.
Here's the documentation: http://msdn.microsoft.com/en-gb/library/ms187746.aspx
If you are storing currency values, then there is the MONEY datatype too: http://msdn.microsoft.com/en-us/library/ms179882.aspx
DISPLAY
If you are more interested in the display of values rather than the storage (You've already mentioned they are floats in the database) then in your C# application you can use something like this:
string display = string.Format("{0:0.00}", myNumber);
Here's the documentation for custom formatting of numbers into strings: http://msdn.microsoft.com/en-gb/library/0c899ak8.aspx
If it is of type float, then X.00f = Xf where X is any integral number.
Yes, it stores 135 or 135.00 - that is the same if it is a float, really.

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

Regex match xml attribute value [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 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(...

How can I replace 40% characters from file ? 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.
I have a text file with characters like this(eg.):
Hi man. how is going. cool. lool. love this thing. fo real. yep.no way.it's real.haha.
The character for this is predetermined and in this case is '.' I'm supposed to be replacing 40% of this character with another character. The 40% of characters are to be chosen at random (only choosing from '.'). How would I go about finding these characters, and then replacing them?
You find the number of ., example: 20 and store the indices of . into an array
You find how many of those you want to delete: example: 8
Then you loop through the array of indices 8 times, get a random one from that array and replace that index with space. Btw, you'll need to remove the updated index from the array at each update.
What you're wanting to do is find all of the indexes of the '.' and store them somewhere, in this case an array. Once you have all of those indexes you take the largest index and use that as the maximum for the random number generator. Divide the largest index by 40 and you have how many times you need to replace a character. Take the random number you've generated and start replacing characters at those indexes.
int i = 0, chr[] = 0;
while (chr[i] != -1)
{
if (string.indexof(specifiedChar, chr[i]) != -1)
{
chr[i] = string.indexof(specifiedChar, chr);
i ++;
}
else
{
chr = -1;
}
}
chr[] will have the indexes of all of your .'s (assumingi specificedChar = '.'). i = number of '.'s. Don't take the above code as ready to roll out, but it's generally correct for your problem.
endInt = i;
int x = RandomNumber.Next(startInt, endInt);
Then replace the character at index[x] with ' '

Categories