What would be the regex to allow digits and a dot? Regarding this \D only allows digits, but it doesn't allow a dot, I need it to allow digits and one dot this is refer as a float value I need to be valid when doing a keyup function in jQuery, but all I need is the regex that only allows what I need it to allow.
This will be in the native of JavaScript replace function to remove non-digits and other symbols (except a dot).
Cheers.
If you want to allow 1 and 1.2:
(?<=^| )\d+(\.\d+)?(?=$| )
If you want to allow 1, 1.2 and .1:
(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )
If you want to only allow 1.2 (only floats):
(?<=^| )\d+\.\d+(?=$| )
\d allows digits (while \D allows anything but digits).
(?<=^| ) checks that the number is preceded by either a space or the beginning of the string. (?=$| ) makes sure the string is followed by a space or the end of the string. This makes sure the number isn't part of another number or in the middle of words or anything.
Edit: added more options, improved the regexes by adding lookahead- and behinds for making sure the numbers are standalone (i.e. aren't in the middle of words or other numbers.
\d*\.\d*
Explanation:
\d* - any number of digits
\. - a dot
\d* - more digits.
This will match 123.456, .123, 123., but not 123
If you want the dot to be optional, in most languages (don't know about jquery) you can use
\d*\.?\d*
Try this
boxValue = boxValue.replace(/[^0-9\.]/g,"");
This Regular Expression will allow only digits and dots in the value of text box.
My try is combined solution.
string = string.replace(',', '.').replace(/[^\d\.]/g, "").replace(/\./, "x").replace(/\./g, "").replace(/x/, ".");
string = Math.round( parseFloat(string) * 100) / 100;
First line solution from here: regex replacing multiple periods in floating number . It replaces comma "," with dot "." ; Replaces first comma with x; Removes all dots and replaces x back to dot.
Second line cleans numbers after dot.
Try the following expression
/^\d{0,2}(\.\d{1,2})?$/.test()
Related
I'm trying to build a regular expression for an abstract filesystem. It should:
Start with letters [a-zA-Z], '/', or '.'
Only allow one consecutive occurrence of '/'
Only allow two consecutive occurrences of '.'
Here's what I have so far (works not allowing 3 '.'s but works when typing only one. Any input is greatly appreciated. I tried positive and negative lookaheads for the second group but it still has the same problem.
(?!.*\/{2})(?!.*\.{3})^[A-Za-z\/\.]*$
My Regex101 link:
https://regex101.com/r/xM8oY5/1
I have added a negative lookahead, that matches a dot . surrounded by two not-dot characters.
/(?!(.*[^.])?\.([^.].*)?$)(?!.*\/{2})(?!.*\.{3})^[A-Za-z\/\.]*$/
^^^^^^^^^^^^^^^^^^^^^^^^^
(.*[^.])? -> some arbitrary characters and at least one not-dot
\. -> the dot
([^.].*)?$ -> one not-dot and some arbitrary characters
Both blocks - before and after the dot - are optional, if the single dot comes at start or end of the string.
Test it on regex101.
I need to extract a strings from a text file that contains both letters and numbers. The lines start like this
Report filename: ABCL00-67900010079415.rpt ______________________
All I need is the last 8 numbers so in this example that would be 10079415
while(<DATA>){
if (/Report filename/) {
my ($bagID) = ( m/(\d{8}+)./ );
print $bagID;
}
Right now this prints out the first 8 but I want the last 8.
You just need to escape the dot, so that it would match the 8 digit characters which exists before the dot charcater.
my ($bagID) = ( m/(\d{8}+)\./ );
. is a special character in regex which matches any character. In-order to match a literal dot, you must need to escape that.
To match the last of anything, just precede it with a wildcard that will match as many characters as possible
my ($bag_id) = / .* (\d{8}) /x
Note that I have also use the /x modifier so that the regex can contain insignificant whitespace for readability. Also, your \d{8}+ is what is called a possessive quantifier; it is used for optimising some regex constructions and makes no difference at the end of the pattern
I have a string like:
$str1 = "12 ounces";
$str2 = "1.5 ounces chopped;
I'd like to get the amount from the string whether it is a decimal or not (12 or 1.5), and then grab the immediately preceding measurement (ounces).
I was able to use a pretty rudimentary regex to grab the measurement, but getting the decimal/integer has been giving me problems.
Thanks for your help!
If you just want to grab the data, you can just use a loose regex:
([\d.]+)\s+(\S+)
([\d.]+): [\d.]+ will match a sequence of strictly digits and . (it means 4.5.6 or .... will match, but those cases are not common, and this is just for grabbing data), and the parentheses signify that we will capture the matched text. The . here is inside character class [], so no need for escaping.
Followed by arbitrary spaces \s+ and maximum sequence (due to greedy quantifier) of non-space character \S+ (non-space really is non-space: it will match almost everything in Unicode, except for space, tab, new line, carriage return characters).
You can get the number in the first capturing group, and the unit in the 2nd capturing group.
You can be a bit stricter on the number:
(\d+(?:\.\d*)?|\.\d+)\s+(\S+)
The only change is (\d+(?:\.\d*)?|\.\d+), so I will only explain this part. This is a bit stricter, but whether stricter is better depending on the input domain and your requirement. It will match integer 34, number with decimal part 3.40000 and allow .5 and 34. cases to pass. It will reject number with excessive ., or only contain a .. The | acts as OR which separate 2 different pattern: \.\d+ and \d+(?:\.\d*)?.
\d+(?:\.\d*)?: This will match and (implicitly) assert at least one digit in integer part, followed by optional . (which needs to be escaped with \ since . means any character) and fractional part (which can be 0 or more digits). The optionality is indicated by ? at the end. () can be used for grouping and capturing - but if capturing is not needed, then (?:) can be used to disable capturing (save memory).
\.\d+: This will match for the case such as .78. It matches . followed by at least one (signified by +) digit.
This is not a good solution if you want to make sure you get something meaningful out of the input string. You need to define all expected units before you can write a regex that only captures valid data.
use this regular expression \b\d+([\.,]\d+)?
To get integers and decimals that either use a comma or a dot plus the next word, use the following regex:
/\d+([\.,]\d+)?\s\S+/
what the regular expression of a line of string containing ONLY float numbers separated with spaces or tabs. The float number can be negative, like -999.999
(?:-?(?:\d+(?:\.\d*)|.\d+)[ \t]*)+
is one possibility. In more readable format:
(?:
-? # Optional negative sign
(?:
\d+(?:\.\d*) # Either an integer part with optional decimal part
|
.\d+ # Or a decimal part that starts with a period
)
[ \t]* # Followed by any number of tabs or spaces
)+ # One or more times
Let's come up with a regex for a float, and then see what we can do about the rest.
A float is:
An optional negative sign
Followed by a number of digits
Followed by an optional decimal point and then more digits
Followed be "e"
Followed by a number of digits (with an optional sign).
Put that together, and we get:
/-?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?/
Now, this is pretty loose, but you can tweak it if you want to tighten it up a little. Now, for any number of these with spaces in between, it's pretty trivial:
/^(F\s+)+$/
Put it all together, we end up with:
/^(-?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?\s+)+$/
A regex for a float would look like this: -?\d+\.?\d+
A whitespace separator looks like this: \s
Put them together, allow it to repeat, make sure the end has a float (not a separator):
((-?\d+\.?\d*)\s)*(-?\d+\.?\d*))
The escaping and \d vs [0-9] might change, depending on your flavor of regex.
I know there are a ton of regex examples on how to match certain phone number types. For my example I just want to allow numbers and a few special characters. I am again having trouble achieving this.
Phone numbers that should be allowed could take these forms
5555555555
555-555-5555
(555)5555555
(555)-555-5555
(555)555-5555 and so on
I just want something that will allow [0-9] and also special characters '(' , ')', and '-'
so far my expression looks like this
/^[0-9]*^[()-]*$/
I know this is wrong but logically I believe this means allow numbers 0-9 or and allow characters (, ), and -.
^(\(\d{3}\)|\d{3})-?\d{3}-?\d{4}$
\(\d{3}\)|\d{3} three digits with or without () - The simpler regex would be \(?\d{3}\)? but that would allow (555-5555555 and 555)5555555 etc.
An optional - followed by three digits
An optional - followed by four digits
Note that this would still allow 555555-5555 and 555-5555555 - I don't know if these are covered in your and so on part
This match what you want numbers,(, ) and -
/^[0-9()-]+$/
^[0-9-+\s]+$
06754654
+54654654
+546 546 5654 43534 +
+09945 345 3453 45
Why do you have a stray ^ in there? I think you meant [()-] This is actually making you have to have two beginning-of-strings in the regex, which will never match.
Also, \d is a nice shortcut for [0-9]. They are exactly the same.
Also, this will only match a bunch of numbers, then a bunch of ( or ) or -. Something like: 1294819024()()()()()-----()- would match. I think you want the whole thing to be able to repeat, something like: ^(\d*[()-]*)*$. Now, you can match repeating sequences of this.
Now, it is important to notice that nested * are typically inefficient, we can realize that we are just wanting to match any digit and the punctuation you want: [\d()-]*
For digits you can use \d. For more than one digit, you can use \d{n}, where n is the number of digits you want to match. Some special characters must be escaped, for example \( matches (. For example: \(\d{3}\)\-\d{3}\-\d{4} matches (555)-555-5555.
The second carat (afaik) is going to break anything you do since it means "start of string".
What you appear to be asking for therefore is:
start of string, followed by...
any number of numeric characters, followed by...
start of string, followed by...
any number of '(',')', or '-' characters, followed by...
end of string
Which won't work even if that second carat does nothing, because you're not accounting for anything after the first '(',')', or '-', and in fact will probably only validate an empty string if that.
You want /^[0-9()-]+$/ for a very crude pattern which will "work".
If you are doing US only number the best solution is to strip out all the non-digit characters and then just test to see if the length == 10.