Regular expression with comma and numbers - c#

I want a Regular expression for a string which can have 4 to 6 digits number and a comma to separate them which is already done by this regular expression:
#"^(\d{1,6},{1})+$"
What i don't want is comma at the end
This existing regular expression works for string of type:0234,23544,234332,
I want a regular expression for following string:0234,23544,234332.

I am still unclear about the (.) at the end or not
but /^\d{1,6},+\d+$/ will match any string as asked not ending with , or .
Demo and Regex Explained

This ^\d{1,6}(,\d{1,6})*$ should do the job. If you want 4-6 digits in a group, replace {1,6} with {4,6}.

i would advice
(\d{4,6}[,|\.])
if you match 'global' e.g. all occurrences you will get this:
0234,
23544,
234332.
0234,
23544,
234332,
take a look here!
for perl it would be
m/(\d{4,6}[,|\.])/g

try this regular expression
^\d{1,6}(,\d{1,6})*(\.)?$

Simplest is probably:
/^(\d{4,6},?\b)+$/

Related

Regex Group Optional

I have the following regular expression that isn't working the way I thought it would.
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2-4})?$");
I am trying to match a string that starts with either 2 or 4 digits, followed by a period, followed by 2 digits and then optionally another period and either 2 or 4 digits.
I would expect 33.44.4444 to work, as would 33.33 but anytime I have a string that has a 2nd period, my expression fails.
What am I doing wrong ?
Your regex is correct for what you want to do except for the {2-4} part, if you use {2,4} it will go for the 2 to 4 characters capture you're looking for.
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2,4})?$");
Hope it helps.
As others have pointed out the syntax {2-4} is incorrect. Use {2,4} to specify a range of occurrences. But also if you only want 2 or 4 (not 3) I would use this regex:
#"^(\d{2}|\d{4})\.\d{2}(\.(\d{2}|\d{4}))?$"
The way you expressed "either two or four digits" in the first section of your expression is correct:
\\d{2}(?:\\d{2})?
The second part does it incorrectly:
(\\.\\d{2-4})?
Copy the first part into the second to fix the problem:
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2}(?:\\d{2})?)?$");
Demo.
You can use this regex:
^\d{2}(?:\d{2})?\.\d{2}(?:\.\d{2}(?:\d{2})?)?$
\d{2-4} will match {2-4} text literally.
RegEx Demo

Regular Expression doesn't Match with string

I am trying to use Regular Expressions to find a string sequence inside a string.
The pattern i am looking for is:
dd.dd.dddd dd:dd:dd //d is a digit from 0-9
my regex is:
Regex r = new Regex(#"(\d[0-9]{2}.\d[0-9]{2}.\d[0-9]{4}\s\d[0-9]{2}:\d[0-9]{2}:\d[0-9]{2})$");
I am now trying to check, if the string "27.11.2014 09:14:59" is Matching to the regex, but sadly it isn't matching.
string str= "27.11.2014 09:14:59";
Regex r = new Regex(#"(\d[0-9]{2}.\d[0-9]{2}.\d[0-9]{4}\s\d[0-9]{2}:\d[0-9]{2}:\d[0-9]{2})$");
test = r.IsMatch(str,0);
//output: test=false
Anyone knows why the String is not Matching with that regular expression?
\d[0-9]{2} matches three digits:
\d first digit
[0-9] second digit
{2} causes the previous expression ([0-9]) to match again
If you remove all occurences of \d, your pattern should work. You should escape all dots . though, because right now they match any character, not just a ..
As Rawing already said, the upper Regular expression is trying to match 3 digits instead of one. for everyone who want to know how the regular expression should look like:
#"(\d{2}.\d{2}.\d{4}\s\d{2}:\d{2}:\d{2})$"
Thats working, at least for me.

How to write regular expression that matches only numbers,text,spaces and only two special characters

How to write regular expression that matches only numbers,text and only these two special characters # and -
[a-zA-Z0-9#-\s]+ anything else should be rejected
You could a regular expression similar to this
^(\d|[a-z]|[A-Z]|#|-)*$
Below the code I used for checking the regular expression in c#:
string input = #"343243-2df---ds#SFD#FD";
string pattern = #"^(\d|[a-z]|[A-Z]|#|-)*$";
Console.WriteLine(Regex.Match(input, pattern).Success);
Console.ReadLine();
Also, for future reference material, you could see this regular expression site: http://www.regular-expressions.info/
I believe the following pattern would work in C#:
string pattern = #"^[a-zA-Z\d\s#-]*$";
I just tried a couple of test cases in C# interactive:
Regex.Match("", pattern).Success
> true
Regex.Match("asdf 2F#-#-", pattern).Success
> true
Regex.Match("asdf~asdf", pattern).Success
> false
If you don't want to accept an empty string, simply change the * to + in the pattern. Also, you didn't state that you wanted to match whitespace in your question but your example pattern does match whitespace. If you don't want to match whitespace, remove the \s.
Update: Edited per Rawling's suggestions.
Try this:
^([a-z]|[A-Z]|[#]|[0-9]|[-])+$

Regular expression for matching start of string with

I need help to built regular expression for
string which does not start with pcm_ or PCM_
any guess!!!
No need to use regular expression. Use String.startsWith() method.
if (!str.StartsWith("pcm_",StringComparison.InvariantCultureIgnoreCase)) {}
if (String.startsWith("pcm_") || String.startsWith("PCM_"))
{
//...
}
The regex solution would be
^(?i)(?!pcm_)
(?i) is the inline version of RegexOptions.IgnoreCase
^ matches the start of the string
(?!pcm_) is a negative lookahead assertion, that is true if the string does not start with "pcm_" or "PCM_" (but also "PcM_, ...)
As already pointed out, you don't need to use regular expressions for this, but if you wanted to you could use one with negative lookahead like so: ^(?!pcm_|PCM_).*$
see similar link
Regex pattern for checking if a string starts with a certain substring?
No need for a Regex here, simply use String.StartsWith http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx

What's the regular expression to determine if the last two characters of string are 'R' and a number?

I need a regex to determine if a given string ends with 'R' and then a single digit (0-9) for instance TESTR3 would be what I'm looking for whereas TESTR34 would not.
Like so:
Regex rx = new Regex("R[0-9]$");
Edit: Reverted to [0-9] to avoid matching characters like ٠١٢٣٤٥٦٧٨٩
The expression R\d$ should work.
Use something like: R[0-9]$
http://regexlib.com/CheatSheet.aspx is always helpful as well.

Categories