I want a regular expression that accepts all numbers, alphabets and only the hyphen (‐) from special characters.
I am trying this expression: ^\d+$/[-]/[a-z] but it does not work. I want to accept expressions like this one:
Emp-IN-0000001
Can someone help me with this?
If it's always this format (Emp-IN-0000001), then use this regexp:
^[a-zA-Z]+-[a-zA-Z][a-zA-Z]-[0-9]+$
or, if you have extended regexps:
^[a-zA-Z]+-[a-zA-Z]{2}-\d+$
when there are always seven digits, use this:
^[a-zA-Z]+-[a-zA-Z]{2}-\d{7}$
You can even say:
^Emp-IN-\d{7}$
if it's exactly "Emp-IN-" + digits.
Btw, this is not C# specific, you can use these regular expressions with any language, as long as they support regexps at all.
If you stickily wants to follow this format Emp-IN-0000001, then you might need to use this regular expression:
^[a-zA-Z]+-[a-zA-Z]+-\d+$
I don't really get what you tried with your regular expression, but it is actually as simple as this:
^[a-zA-Z\d-]+$
Or if you want to allow empty strings:
^[a-zA-Z\d-]*$
If you use the case-insensitive modifier with your regular expression, you can leave out either the a-z or A-Z from both variants.
I recommend you read up on some regex basics in this great tutorial.
Related
I am extracting all numbers used in an xml file. The numbers are written in following two patterns
<Environment Id="11" StringId="8407" DescriptionId="5014" RemoteControlAppStringId="8119; 8118" EnvironmentType="BlueToothBridge" AlternateId="1" XML_NAME_ID="BTBSpeechPlusM" FactoryGainType="LIN18">
<Offsets />
</Environment>
I am using regex: "\"\d*;\"" and "\"\d*\"" to extract all numbers.
from the above when i ran Regex "\"\d*\"" using
Regex.Match(myString, "\"\\d*\"")
the above line returns 8407, 11,5014 but it is not returning 8119 and 8118
Your regex will fail to match 8119; 8118 because your pattern is finding quoted numbers.
try with
\b\d+\b
\b specify that \d+ will match only in word boundary. So LIN18 will not match.
Depening on whether you can assume that the provided input is valid XML, you could use the following regular expression:1
Regex.match(myString, "(?<=\")\\d+(?=\")|(?<=\")\\d+(?=; ?\\d+\")|(?<=\"\\d+; ?)\\d+(?=\")" )
The main idea behind this is that it takes the three possible situations into account:
"[number]"
"[number]; [other_number]" (With or without a space before [other_number])
"[other_number]; [number]" (With or without a space before [number])
There are two new concepts I included in the regular expression:2
Positive lookahead: (?=[regex])
Positive lookbehind: (?<=[regex])
These concepts allow the regular expression to check if something specific is before or after it, without putting it in the match.
This regular expression could easily be optimised, but this is meant as an example of a basic approach.
One good tip for developing a regular expression like this is to use a tool (online or offline) to test your regular expression. The tool I used was .NET Regex Tester.
As #poke stated in the comment, it's because your regex doesn't match the string. Change your regex to capture specific matches and account for the possibility of the ';'.
Something like below should probably do the trick.
EDIT: (\b\d+\b)|(\b\d+[;*]\d+\b)
I have no knowledge of regular expressions and find the documentations so hard to understand.
Currently I use this expression
#"\d+(\R.\d{0,2})?"
It only allows decimals which is what I want but it does not allows negative numbers.
I found this question about the same subject :
How do I include negative decimal numbers in this regular expression?
but I just cannot see what I need to change in my expression to get it working.
I would appreciate some help with this.
If there is some documentation on the subject that is clear to read and understand that would also be nice.
Use ^-?\d+(?:\.\d{0,2})?$, but your regex allows numbers like 20. so i suggest to chage it at least for this one ^-?\d+(?:\.\d{1,2})?$.
Also don't forget the ^ and the $. You can use www.regex101.com/, where you can try regex and watch a good documentation.
you can include the - as
#"[+-]?\d+(\R.\d{0,2})?"
Check this simple Cheat sheet for C# regular expressions metacharacters, operators, quantifiers etc
and For sure https://regex101.com is the best place Online regex tester
I need regular expression (C#) for symbol * - it should match any number of any characters, but it can contain only one space. I tried following, but its not working:
#".*[^[\t\0x0020]^[\t\0x0020]+].*"
#".*[^\s^\s+].*"
#".*[^\s\s+].*"
any way how to create regex like this?
Example: If user write expression MTN*-* it has to match for example
MTN3111-0000
but not
MTN311100 MTN3111-0000
You could use this expression:
\S*\s?\S*
It would match any number of any characters, but allow at most one space.
Here:
#"[^\s]*\s?[^\s]*"
Then there may be some specifics depending on other requirements
here try this:
([^\-]+\-[^\s]+)\s
Need a regular expression to replace the following sequence
Before : abbbccdd After : abcd
And also if numeric data is present instead of alphabets i would like to remove the duplicates and display
For the first part, in most languages you can do something like replacing (.)\1+ with $1.
The exact syntax depends on the language and the regular expression engine you are using, so check the manual for your language for more details.
This works in PHP:
preg_replace('/(.)\1+/','$1',$str);
I'm not sure what you mean with your second question, though.
Maybe this is a very rare (or even dumb) question, but I do need it in my app.
How can I check if a C# regular expression is trying to match 1-character strings?
That means, I only allow the users to search 1-character strings. If the user is trying to search multi-character strings, an error message will be displaying to the users.
Did I make myself clear?
Thanks.
Peter
P.S.: I saw an answer about calculating the final matched strings' length, but for some unknown reason, the answer is gone.
I thought it for a while, I think calculating the final matched strings length is okay, though it's gonna be kind of slow.
Yet, the original question is very rare and tedious.
a regexp would be .{1}
This will allow any char though. if you only want alpanumeric then you can use [a-z0-9]{1} or shorthand /w{1}
Another option its to limit the number of chars a user can type in an input field. set a maxlength on it.
Yet another option is to save the forms input field to a char and not a string although you may need some handling around this to prevent errors.
Why not use maxlength and save to a char.
You can look for unescaped *, +, {}, ? etc. and count the number of characters (don't forget to flatten the [] as one character).
Basically you have to parse your regex.
Instead of validating the regular expression, which could be complicated, you could apply it only on single characters instead of the whole string.
If this is not possible, you may want to limit the possibilities of regular expression to some certain features. For instance the user can only enter characters to match or characters to exclude. Then you build up the regex in your code.
eg:
ABC matches [ABC]
^ABC matches [^ABC]
A-Z matches [A-Z]
# matches [0-9]
\w matches \w
AB#x-z matches [AB]|[0-9]|[x-z]|\w
which cases do you need to support?
This would be somewhat easy to parse and validate.