I’m trying to figure out the regular expression for “X.X”, where ‘X’s are numbers.
Some valid values:
1.1
2.0
0.1
9.1
9.9
Some invalid values:
10.1
9.22
1
10
Looking at your examples this regex should work:
/^\d\.\d$/
Live Demo: http://www.rubular.com/r/vdemgjS9NY
I'm no regex expert,
but "^\d.\d$"
seems to work for me when I test on Rubular for a line with only X.X and nothing more.
Related
I am looking for regex expression which takes -10 to 10 with 2 decimal numbers upto 12. means cases passed are -10.01 to -10.11 for 10 years and 11 months.
I was able to make a regex for -10 to 10 but adding decimal till .11 was not able to figure out If somebody can help that would be great.
cases passed -
0
1
10.01
10.00
10.11
-10.00
-10.01
10.12
-10.00
10
cases should fail -
10.12
10.13
-10.13
11
-11
-10.13
I presume that values such as 9.08 and -5.04 are also valid? In that case this regex: ^-?(10|[0-9])(\.(0[0-9]|1[01]))?$ will match the values you want. The first part looks for a number from -10 to 10; the second part looks for an optional decimal part which can be something from 00-09 or 10 or 11.
Regex101 demo
I am looking for a regular expression to match floating point numbers with at most 4 digits before the '.' and at most 4 digits after.
Examples of valid inputs:
1234.5678
123.567
12.34
1.2
1.23
12.3
etc...
There are many places where you get the answer for it. Here is your required answer,
^\d{0,4}(\.\d{0,4})?$
Test the answer in this Link
Given the below text, I want to return an array of all the the lines of text with the following format 1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN- 1.1 and this should macth a line even if it is actually broken across multiple lines
RegExp:
str.match(/\d{1,2}.SSRDOCSYYHK1\/\/\/\/\/.+?\d\.\d/g)
Full Text:
var str= "A-CA25592185
A-ERNONREF/CHGFEEPLUSFAREDIF/CXL BY FLT TIME NOVALUE
TKG FAX-NOT PRICED FARE TYPE EX
FOP- 1.CA
G- 1.SSRDOCSWSHK1/////25MAY55/M//YOUNG/LANDON/KWAN - 1.
1
)>MD
2.SSRPSPTYYHK1///25AUG52/M- 1.1
3.SSRDOCSWSHK1/////25AUG52/F//YOUNG/LILY/LIMKUO - 2.1
4.SSRPSPTYYHK1///25AUG52/F- 2.1
5.SSRDOCSWSHK1/////25AUG52/F//YOUNG/ANDREA/LAUREN - 3.1
6.SSRPSPTYYHK1///25AUG52/F- 3.1
7.SSRDOCSWSHK1/////17MAR93/M//YOUNG/ETHAN/WESLEY - 4.1
8.SSRPSPTYYHK1///25AUG52/M- 4.1
9.SSRDOCSWSHK1/////23NOV96/M//YOUNG/WINSTON/JEREMY - 5.1
10.SSRPSPTYYHK1///25AUG52/M- 5.1
11.SSRDOCSYYHK1/////25MAY55/M//YOUNG/LANDON/KWAN - 1.
1
12.SSRDOCSYYHK1/////04MAR59/F//YOUNG/LILY/LIMKUO - 2.1
13.SSRDOCSYYHK1/////25AUG52/F//YOUNG/ANDREA/LAUREN - 3.1
)>MD
7.SSRDOCSWSHK1/////25AUG52/M//YOUNG/ETHAN/WESLEY - 4.1
8.SSRPSPTYYHK1///25AUG52/M- 4.1
9.SSRDOCSWSHK1/////25AUG52/M//YOUNG/WINSTON/JEREMY - 5.1
10.SSRPSPTYYHK1///25AUG52/M- 5.1
11.SSRDOCSYYHK1/////25MAY55/M//YOUNG/LANDON/KWAN - 1.
1
12.SSRDOCSYYHK1/////25AUG52/F//YOUNG/LILY/LIMKUO - 2.1
13.SSRDOCSYYHK1/////25AUG52/F//YOUNG/ANDREA/LAUREN - 3.1
14.SSRDOCSYYHK1/////25AUG52/M//YOUNG/ETHAN/WESLEY - 4.1
15.SSRDOCSYYHK1/////25AUG52/M//YOUNG/WINSTON/JEREMY - 5.1
**** ITEMS SUPPRESSED ****/DR"
I expect an array with all the matches but the two instances of line 11 are not matched due to the line break which can occur in any of the below way and will not currently be matched:
var str="1.SSRDOCSYYHK1/////25AUG52/M//
YOUNG/LANDON/KWAN- 1.1"
var str="1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN- 1.
1"
var str="1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN- 1
.1"
var str="1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN-
1.1"
var str="1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN
- 1.1"
How change I tell this RegExp to still match in all of the above cases?
I did try str.match(/\d{1,2}.SSRDOCSYYHK1\/\/\/\/\/.+?\d\.\d/m) with no luck
Heres the array that I do get:
anubhava's answer below returns the following array, note that slots 0 and 2 actually hold two lines that were captured as a single instance. This always happens when a line breaks like this and is followed by another matching line if I use his example.
If it can be broken anywhere, not only in the DOT matching, the \s trick won't work.
I don't think there's a way to ignore line breaks in javascript regex (or any other engine, actually).
Your best option would be to remove all line breaks before matching, like so:
str = str.replace(/(\r\n|\n|\r)/gm,"");
And then you .match
Ok. below regular work for 1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN- 1.1
[0-1]\.[A-Z]+[0-1]\/\/\/\/\/[0-9]+[A-Z]+[0-9]+\/[A-Z]\/\/[A-Z]+\/[A-Z]+\/[A-Z]+\-\s[0-1]\.[0-1]
And it work for :
1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN- 1.
1
[0-1]\.[A-Z]+[0-1]\/\/\/\/\/[0-9]+[A-Z]+[0-9]+\/[A-Z]\/\/[A-Z]+\/[A-Z]+\/[A-Z]+\-\s[0-1]\.\n[0-1]
And it work for :
1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN- 1
.1
[0-1]\.[A-Z]+[0-1]\/\/\/\/\/[0-9]+[A-Z]+[0-9]+\/[A-Z]\/\/[A-Z]+\/[A-Z]+\/[A-Z]+\-\s[0-1]\n\.[0-1]
And it work for :
1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN-
1.1
[0-1]\.[A-Z]+[0-1]\/\/\/\/\/[0-9]+[A-Z]+[0-9]+\/[A-Z]\/\/[A-Z]+\/[A-Z]+\/[A-Z]+\-\n[0-1]\.[0-1]
And it work for :
1.SSRDOCSYYHK1/////25AUG52/M//YOUNG/LANDON/KWAN
- 1.1
[0-1]\.[A-Z]+[0-1]\/\/\/\/\/[0-9]+[A-Z]+[0-9]+\/[A-Z]\/\/[A-Z]+\/[A-Z]+\/[A-Z]+\n\-\s[0-1]\.[0-1]
now you should define nested if and else .....(Conditional statements)
good look.
DOT in Javascript doesn't match new lines and unfortunately there is no DOTALL switch in JS regex engine.
However as a workaround you can use [\s\S] for DOT and match regex across new lines as well.
Following regex should work for you:
var arr = str.match(/\d{1,2}\.SSRDOCSYYHK1\/{3,5}[\s\S]+?\d\.\d/g);
Live Demo: http://ideone.com/QIYCMA
I'm using plain vanilla JavaScript and need some help with my regex. Money in the following formats has to be allowed, and in these formats only (with no limit on the number of 0s (tens, hundreds, thousands, etc.) for the dollar amounts allowed):
$25,000
$25000
25,000
25000
25000.01
25,000.99
2000.99
50.00
50
1.95
1 .99
0.25
$0.25
0.2
2.3
2000.5
.75
var regex = /^\$?.?[1-9][0-9,]*(.[0-9]{0,2})?$/;
Currently, it's not allowing amounts like 0.99 to be entered.
Try this
^\$?(?:\d+|\d{1,3}(?:,\d{1,3})*)(?:\.\d{2})?$
See it here on Regexr
The only thing that is is not matching is your third last example, it has a space before the dot. Is that valid?
Edit:
My frist solution has the restriction, that it would accept numbers starting with 0, like 001. This solution uses a negative lookahead to avoid this:
^\$?(?!0\d)(?:\d+|\d{1,3}(?:,\d{1,3})*)(?:\.\d{2})?$
See it here on Regexr
Solution without lookahead
^\$?(?:0|[1-9]\d*|[1-9]\d{0,2}(?:,\d{1,3})*)(?:\.\d{2})?$
See it on Regexr
^\$?(?:[1-9]\d?\d?(?:(?:,\d{3})*|(?:\d{3})*)|0)(?:\.\d\d?)?$
Altho I wouldn't use such strict input restrictions.
I need a regular expression in javascript that will accept only positive numbers and decimals. This is what I have but something is wrong -- it doesn't seem to take single positive digits.
/^[-]?[0-9]+[\.]?[0-9]+$/;
For example, 9 will not work. How can I restructure this so if there is at least one positive digit, it will work?
/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/
matches
0
+0
1.
1.5
.5
but not
.
1..5
1.2.3
-1
EDIT:
To handle scientific notation (1e6), you might want to do
/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/
If you want strictly positive numbers, no zero, you can do
/^[+]?([1-9][0-9]*(?:[\.][0-9]*)?|0*\.0*[1-9][0-9]*)(?:[eE][+-][0-9]+)?$/
There are few different ways to do this depending on your need:
/^[0-9.]+$/ matches 1 and 1.1 but not -1
/^[0-9]+\.[0-9]+$/ matches 1.1 but not 1 or -1
Generally, I recommend using a simple regExp reference guide like http://www.regular-expressions.info/ for building expressions, and then test them using javascript right your browser console:
"123.45".match(/^[0-9.]+$/)
You can try this -
^\d{0,10}(\.\d{0,2})?$
Also one cool site to test as well as to get description of your own regular expressions
https://regex101.com/
How about like:
^[.]?[0-9]+[.]?[0-9]*$
I have found something interesting which work for me!!!
hope so for you also...!!!
[0-9].[0-9]
Regex for accepting only a positive number with a decimal point
Matches:
1, 2564, 2.5545, 254.555
Not Matches:
-333, 332.332.332, 66-665.455, 55554-4552
I have done one in case people are interested:
^([0-9]*)(.[[0-9]+]?)?$
but it doesn't have '+' sign
while also it doesn't accept '-' sign
It accepts:
0
0.5
1.0
1.5
2.0
2.5
3.0
1
2
3
4
0.0
0.1
0.9
0.4
1.3
1.11
1.51
2.01
01.5
0.51
1.1
.1
Reject:
-1.0
-0.5
-0.0
-0.1
-0.7
11123.
1.001.
.1.1