C# Regex for three digit and an alpha - javascript

I have a field where I need to have a regex where the first 3 digits are numeric and the fourth character should be alpha- letter only, I need to have regex in both c# and javascript.
My following regex is good for three numeric number
#"\A(\d){3}\Z";
How to add for the fourth character which has to be alpha

If by alpha you mean only latin letters, you can do this:
^\d{3}[a-zA-Z]$
You can't use \A and \Z in JavaScript but they're equivalent to ^ and $ unless you use the m option.
If you need full Unicode character range, use \p{L} instead of [a-zA-Z], but you're out of luck for JavaScript support. You'd have to include the relevant Unicode ranges by hand into the character class...

I think what you want to know about is Character Classes or Character Sets
With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. [...]
and
[...] You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9. You can use more than one range. [0-9a-fA-F] matches a single hexadecimal digit, case insensitively. You can combine ranges and single characters. [0-9a-fxA-FX] matches a hexadecimal digit or the letter X. Again, the order of the characters and the ranges does not matter. [...]
So basically you can match character "A" to "Z" by placing them in square brackets and using a hyphen to indicate range
[A-Z]
You can match multiple sets, so if you also need "a" to "z" (lowercase) you can include
[A-Za-z]

Related

RegEx start and finish with letter, allow commas and dashes

I've got this regex:
/^[\a-zøåæäöüß][\a-z0-9øåæäöüß]*(?:\-?[\a-z0-9øåæäöüß,]+)*$/i
It works fine for a crazy input like "K61-283øÅ,æk-ken,a-sd", but it fails on the cases "word," (so, when there's just one comma).
Also - how can I restrict it that it should start with a letter after every comma or dash (so basically - every word)?
The rule is: start with a letter and end with alphanumeric; allow alphanumeric, dashes and commas; after each dash or comma there should be a letter
You may use
/^[a-zøåæäöüß][a-z0-9øåæäöüß]*(?:[-,][a-zøåæäöüß][a-z0-9øåæäöüß]*)*$/i
See the regex demo
Details:
^ - start of string
[a-zøåæäöüß] - a letter from the defined set
[a-z0-9øåæäöüß]* - 0+ digits or letters from the defined set
(?:[-,][a-zøåæäöüß][a-z0-9øåæäöüß]*)* - zero or more sequences of:
[-,] - a - or ,
[a-zøåæäöüß] - a letter from the defined set
[a-z0-9øåæäöüß]* - 0+ digits or letters from the defined set
$ - end of string.
Update 2:
There are two ways to look at your requirements.
The top-down view
We treat the input as a list of one or more words, separated by comma or dash:
INPUT = WORD (?: [,\-] WORD )*
Each word consists of a letter, followed by zero or more letters or digits:
WORD = LETTER [ LETTER DIGIT ]*
Translated into JavaScript regex syntax this gives us:
WORD = [a-zøåæäöüß][a-zøåæäöüß\d]*
And for the whole input (with anchors):
/^[a-zøåæäöüß][a-zøåæäöüß\d]*(?:[,\-][a-zøåæäöüß][a-zøåæäöüß\d]*)*$/i
(This is Wiktor Stribiżew's answer.)
The bottom-up view
We start by looking at the allowed characters. We know that the first character has to be a letter. After that, there can be zero or more input elements:
INPUT = LETTER ELEMENT*
Each element is either
a letter or digit, or
a comma or dash, followed by a letter:
ELEMENT = [ LETTER DIGIT ] | [ COMMA DASH ] LETTER
Translating this into JavaScript gives us:
/^[a-zøåæäöüß](?:[a-zøåæäöüß\d]|[,\-][a-zøåæäöüß])*$/i
These two regexes are equivalent. The bottom-up regex is shorter and contains less repetitive code. On the other hand, the top-down regex may run faster on some regex engines if the input strings are mostly alphanumeric, with relatively few dashes/commas. On the gripping hand, if your inputs are short, you probably don't care about minuscule performance differences.
Here's a direct encoding of your (revised) requirements:
/^[a-zøåæäöüß](?:(?:[a-zøåæäöüß\d]|[,\-][a-zøåæäöüß])*[,\-]?[a-zøåæäöüß])?$/i
The idea is to match a letter, followed by either
the end of the string (this handles input strings of length 1), or
a list of 0 or more intermediates, optionally followed by a comma or dash, followed by another letter
Each intermediate is either
a letter, or
a digit, or
a comma or a dash followed by a letter
Try this out: (allows letters and digits after comma or dash)
/^[a-zøåæäöüß]([a-z0-9øåæäöüß]|(,|-)[a-z0-9øåæäöüß])*[a-zøåæäöüß]$/i
or this: (allows letters after comma or dash)
/^[a-zøåæäöüß]([a-z0-9øåæäöüß]|(,|-)[a-zøåæäöüß])*[a-zøåæäöüß]$/i

Regular expression X characters long, alphanumeric but not _ and periods, but not at beginning or end

As the subject indicates, I am in need of a JavaScript Regular expression X characters long, that accepts alphanumeric characters, but not the underscore character, and also accepts periods, but not at beginning or end. Periods cannot be consecutive either.
I have been able to almost get to where I want to be searching and reading other people's questions and the answers here on Stack Overflow (such as here).
However, in my case, I need a string that has to be exactly X characters long (say 6), and can contain letters and numbers (case insensitive) and may also include periods.
Said periods cannot be consecutive and also, cannot start, or end the string.
Jd.1.4 is valid, but Jdf1.4f is not (7 characters).
/^(?:[a-z\d]+(?:\.(?!$))?)+$/i
is what I have been able to construct using examples from others, but I cannot get it to only accept strings that match the set length.
/^((?:[a-z\d]+(?:\.(?!$))?)+){6}$/i
works in that it now accepts nothing less than 6 characters, but it also happily accepts anything longer as well...
I am obviously missing something, but I do not know what it is.
Can anyone help?
This should work:
/^(?!.*?\.\.)[a-z\d][a-z\d.]{4}[a-z\d]$/i
Explanation:
^ // matches the beginning of the string
(?!.*?\.\.) // negative lookahead, only matches if there are no
// consecutive periods (.)
[a-z\d] // matches a-z and any digit
[a-z\d.]{4} // matches 4 consecutive characters or digits or periods
[a-z\d] // matches a-z and any digit
$ // matches the end of the string
Another way to do that:
/(?=.{6}$)^[a-z\d]+(?:\.[a-z\d]+)*$/i
explanation:
(?=.{6}$) this lookahead impose the number of characters before
the end of the string
^[a-z\d]+ 1 or more alphanumeric characters at the beginning
of the string
(?:\.[a-z\d]+)* 0 or more groups containing a dot followed by 1 or
more alphanumerics
$ end of the string

RegExp extract specific string followed by any number with leading / trailing whitespace

I want to extract a string from another using JavaScript / RegExp.
Here is what I got:
var string = "wp-button wp-image-45 wp-label";
string.match(/(?:(?:.*)?\s+)?(wp-image-([0-9]+))(:?\s(?:.*)?)?/);
// returnes: ["wp-button ", "wp-image-45", "45", undefined]
I just want to have "wp-image-45", so:
(Optional) any character
(Optional) followed by whitespace
(Required) followed by "wp-image-"
(Required) followed by any number
(Optional) followed by whitespacy
(Optional) followed by any character
What is missing here? Is it just some kind of bracketing or more?
I also tried
string.match(/(?:(?:.*)?\s+)?(?=(wp-image-([0-9]+)))(?=(:?\s(?:.*)?)?)/)
Edit: In the end I just want to have the number. But I'd also make this step in between.
Regexps are not required to start matching at the beginning of the string, so your attempts to match whitespace and any character aren't necessary. Also, "any character" includes whitespace (except newlines in certain modes).
This should be all you need:
string.match(/\bwp-image-(\d+)\b/)
This will capture, for example, "wp-image-123" into matching group 0, and "123" into matching group 1.
\b means "word boundary", which ensures that you won't match "abcwp-image-123def". A word boundary is defined as any place where a non-word character is followed by a word character, or vice versa. A word character is consists of a letter, a number or an underscore.
Also, I used \d instead of [0-9] simply out of convenience. They have slightly different meaning (\d also matches characters considered numbers in other languages), but that won't make a difference in your case.
If all of that surrounding stuff is optional and all you want is the number then there's no point to matching for any of that stuff except for that "wp-image-" prefix, just do:
var string = "wp-button wp-image-45 wp-label";
string.match(/wp-image-([0-9]+)/);

How to write Regular expression for minimum one character in javascript?

I have small requirement in Regular expression,here I need minimum of one letter of Alphabets and followed by numbers and special characters. I tried the following regular expressions but I'm not getting the solution.
/^[a-zA-Z0-9\-\_\/\s,.]+$/
and
/^([a-zA-Z0-9]+)$/
I need minimum of one letter of Alphabets
[a-z]+
and followed by numbers and special characters.
[0-9_\/\s,.-]+
Combined together you would get this:
/^[a-z]+[0-9_\/\s,.-]+$/i
The /i modifier is added for case insensitive matching of alphabetical characters.
Try this regex:
/^[a-z][\d_\s,.]+$/i
To clarify what this does:
^[a-z] // must start with a letter (only one) add '+' for "at least one"
[\d_\s,.]+$ // followed by at least one number, underscore, space, comma or dot.
/i // case-insensitive
You need the other character selection to be separate. I'm confused as to what "numbers and special characters" means, but try:
/^[a-z]+[^a-z]+$/i

How to make the below regex to accept any special character

/^[^ ]([\w- \.\\\/&#]+)[^ ]$/,
I have the above regex. I want to make sure it accepts all special characters but i don't want to specify the entire special character listsuch as [\w- \.\\\/&#!##$&]. How can we make sure the above regex accepts all special characters
[^\w\s] matches any non-alphanumeric and non-whitespace character.
\S matches any non-whitespace character.
. matches any character except newlines.
[\S\s] matches any character in a JavaScript regex.
Since you've got \w and a space in there already, you must want all of the ASCII characters except control characters. That would be:
[ -~]
...or any character whose code point is in the range U+0020 (space) to U+007E (tilde). But it looks like you want to make sure the first and last characters are not whitespace. In fact, looking at your previous question, I'll assume you want only letters or digits in those positions. This would work:
/^[A-Za-z0-9][ -~]*[A-Za-z0-9]$/
...but that requires the string to be at least two characters long. To allow for a single-character string, change it to this:
/^[A-Za-z0-9](?:[ -~]*[A-Za-z0-9])?$/
In other words, if there's only one character, it must be a letter or digit. If there are two or more characters, the first and last must letters or digits, while the rest can be any printing character--i.e., a letter, a digit, a "special" (punctuation) character, or a space.
Note that this only matches ASCII characters, not accented Latin letters like  or ë, or symbols from other alphabets or writing systems.
. matches any character except for newline.

Categories