regex 1-9999 for form validation - regex

I am trying to write some form validation, I need one of the inputs to be 1-9999. I know nothing about regular expressions ( never used them before) and here is my first attempt
/^([1-9][1-9]|[1-9]|[1-9]\d|9999)$/
Does not seem to want to work, can anyone help me? Thanks!

Try the below regex,
^(?:[1-9][0-9]{3}|[1-9][0-9]{2}|[1-9][0-9]|[1-9])$
DEMO

This doesn't exclude zero, but /^\d{1,4}$/ should do the trick.

Try using this
^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$

To exclude zero values but include non-zero ones with leading zeros:
([1-9]\d{0,3})|(\d[1-9])\d{0,2}|(\d{2}[1-9])\d?|(\d{1,3}[1-9])

This Regex should not match numbers that start with 0 a part from 0
Regex: /^(?!(0\d))\d{1,4}$/
Regex (Exclude Zero): /^(?!(0))\d{1,4}$/
Test: https://regex101.com/r/zfCKel/2

Related

Regex: find only numbers in a string

I have a string
K9908098F, G2342D34324/ 234234323, 234-234-234; R324234
How to catch only 234234323 and 234-234-234 in VBA?
This [\d-]+ pattern grabs extra pieces
You are pretty close, just need to add borders: \b[\d-]+\b
Regex demo and explanation
Give this a try:
(\w+),\s+([\w-]+);
This will capture 234234323 in group 1 and 234-234-234 in group 2.
Not too elegant though but would work. Just a small addition to your regex.
[\d-]+[,;]
You can try this too,
[-\d]+(?=[;,. ])
Demo

Regex to validate this format

I'm trying to write a regex to validate a specific format. Here is it:
key=0(or)1;key=0(or)1;(repeated-or-not);key=0(or)1.
Or said otherwise:
\w
sign
0 or 1
; sign only between elements. Not on the last element.
All of previous repeated, or not.
The specificity is that the string can not end with a ";".
For now I've this ^(?:[a-z]+=[01];?)+(?<!;)$ which is right but not completely. since foo=1;bar=0foo=0;bar=1passes but even tough this part bar=0foo=0is incorrect.
Here are my current regex and some testing strings: https://regex101.com/r/lX0xT7/1
Thank you for your help,
Cheers,
You can use this regex:
^\w+=[01](?:;\w+=[01])*$
Updated RegEx Demo
Modifying your original regex-
^(?:[a-z]+=[01];)*(?:[a-z]+=[01])$
Demo

Easy regular expression- please correct

I have AUD0.7195.
I want to mach AUD in $1 and 0.7195in $2.
The amount may vary. It can be positive integer.
I have tried with:
/([A-Z]{3})(\d{+}\.?\d*)/ms;
But it matches nothing. What is wrong? Thanks!
([a-zA-Z]+)(\d+(?:\.\d+)?)
Use this.Yours does not work as \d{+} does not act as quantifier.
\d{+} doesn't make sense and should be \d+
Could you please try this:
my $string = "AUD0.7195";
print "$1\t$2.......\n", if($string=~m/^([\w]{3})([0-9\-\.]*)$/g);

Regular Expression Filter for values in brackets

I've been trying to get the correct filter for:
{0}{1/2}{R/G}{X}{Y}{Z}{R}{R}
I've tried this on rubular.com (http://rubular.com/r/niCiKoUfmN):
\{([0-Z])\}
I get:
{0}{X}{Y}{Z}{R}{R}
But I do not get:
{1/2}{R/G}
How can I write the regular expression so it gets all of it?
\{(\w)(?:\/(\w))?\}
Edit live on Debuggex
A radical way consists to use a negated character class with the character you want to avoid:
\{([^}]*)\}
[^}] means all characters except }
* means zero or more times
You don't have the slash sign (/) in your group. Further, you have to add an quantificator to tell the parser, more characters in brackets are allowed:
\{([0-Z/]+)\}
You can do so by adding an optional /[0-Z]
Which will give you:
\{([0-Z](\/[0-Z])?)\}
Rubular: http://rubular.com/r/3D0VPCaJX7
This should do it:
\{[0-Z\/]+\}
You don't need the parentheses unless you're wanting to use a subset of the match for something else.
You need to include 0 or more inclusions of the / clause.
Debuggex Demo
\{([0-Z][\/0-Z]*)\}
Edit live on Debuggex
jsFiddle Demo in javascript

Regex pattern to match this pattern

I have the following string: LLLTTTLTLLLTT. The number of L's and T's are dynamic.
I tried to use regex "L+T+". It's not working as the number of occurrences is not only once. What is the right regex pattern to match?
Or try this regexp:
([LT]+)
The ( ) are not necessary depending what you want to achieve
Unless there is a clear requirement that the string cannot be empty, it is [LT]*.
Try this:
(L+T+)+
Basically, you are matching the patter "L+T+" more than one time.
How about
(L?T?)+
I am assuming that the string can start either with a L or a T
Or as user1290772 has pointed out, the equivalant would be
[LT]+
Try this:
(L+T+)+
Which means a combination of Ls and then Ts one or more times.
Bu this promises that it starts with L.
If you want either, try:
[LT]+