This is a sample text i'm running my regex on:
DuraFlexHose Water 1/2" hex 300mm 30.00
I want to include everything and stop at the 30.00
So what I have in mind is something like [^\d*\.\d*]* but that's not working. What is the query that would help me acheive this?
See Demo
/.*(?=\d{2}\.\d{2})/
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
(?=\d{2}\.\d{2}) Positive Lookahead - Assert that the regex below can be matched
\d{2} match a digit [0-9]
Quantifier: {2} Exactly 2 times
\. matches the character . literally
\d{2} match a digit [0-9]
Quantifier: {2} Exactly 2 times
If you cannot use any CSV parser and are only limited to regex, I'd suggest 2 regexps.
This one can be used to grab every character from the beginning up to the first pattern of optional spaces + digits(s) + . + digit(s):
^([\s\S]*?)\s*\d+\.\d+
See demo
In case the float value is at the end of string, use a $ anchor (the end of string):
^([\s\S]*?)\s*\d+\.\d+$
See another demo
Note that [\s\S] matches any symbol, even a newline.
Regex breakdown:
^ - Start of string
([\s\S]*?) - (Capture group 1) any symbols, 0 or more, as few as possible otherwise, 3 from 30.45 will be captured)
\s* - 0 or more whitespace, as many as possible (so as to trim Group 1)
\d+\.\d+ - 1 or more digits followed by a period followed by 1 or more digits
$ - end of string.
If you plan to match any floats, like -.05, you will need to replace \d+\.\d+ with [+-]?\d*\.?\d+.
Here is how it can be used:
var str = 'DuraFlexHose Water 1/2" hex 300mm 300.00';
var res = str.match(/^([\s\S]*?)\s*\d+\.\d+/);
if (res !== null) {
document.write(res[1]);
}
Related
I'm new to regular expressions and trying to figure out which expression would match 1,2,3 and 6,7,8,9th letter in the string, starting from the end of the string. It would also need to include \D (for non-digits), so if 3rd letter from the end is a number it will exclude it.
Example of a string is
Wsd-kaf_23psd_trees32rap
So the result should be:
reesrap
or for
Wsd-kaf_23psd_trees324ap
it would be
reesap
This
(?<=^.{9}).*
gives me last 9 chars, but that's not really what I want.
Does anyone knows how can I do that?
Thanks.
You could try to use alternations to find all characters upto the position that holds 9 character untill the end or consecutive digits:
(?:^.*(?=.{9})|\d+)
See an online demo. Replace with empty string.
(?: - Open non-capture group;
^.* - Any 0+ characters (greedy), upto;
(?=.{9}) - A positive lookahead to assert position is followed by 9 characters;
| - Or;
\d+ - 1+ digits.
If, however, your intention was to match the characters seperately, then try:
\D(?=.{0,8}$)
See an online demo. Any non-digit that has 0-8 characters upto the end-line character.
I have an application that needs to handle validation for phone numbers. Phone numbers are required to have 13 characters (sum of numbers and dashes). There must be at least 1 dash and a maximum of 3 dashes. The starting character must be a digit. How can I create a regex for this validation? Here is my regex string. /^(?:[0-9]-*){13}$/ It doesn't work exactly what I expected
So 13 characters in total with a maximum of 3 dashes and a minimum of 1 means 10 digits right? Therefor your characters are ranging 11-13?
If so, try:
^(?=(?:\d-?){10}$)\d+(?:-\d+){1,3}
See an online demo
^ - Start line anchor.
(?= - Open a positive lookahead:
(?: - Open a non-capture group:
\d-? - Match a digit and optional hyphen.
){10}$) - Close the non-capture group and match it ten times before the end-string anchor. Then close the lookahead.
\d+ - 1+ Digits.
(?: - Open a 2nd non-capture group:
-\d+ - Match an hyphen and 1+ digits.
){1,3} - Close non-capture group and match it 1-3 times.
You can use
^(?=.{13}$)[0-9]+(?:-[0-9]+){1,3}$
^(?=.{13}$)\d+(?:-\d+){1,3}$
See the regex demo. Details:
^ - start of string
(?=.{13}$) - the string must contain exactly 13 chars
[0-9]+ / \d+ - one or more digits
(?:-[0-9]+){1,3} / (?:-\d+){1,3} - one, two or three repetitions of a hyphen followed with one or more digits
$ - end of string.
See the regex graph:
A JavaScript demo:
const texts = ['123-4567-8901','123-45-67-890','123-456728901','1234567890123','123--67890123','-234567890123','123456789012-','-23456789012-'];
const regex = /^(?=.{13}$)\d+(?:-\d+){1,3}$/;
for (const text of texts) {
console.log(text, "=>", regex.test(text));
}
I assumed the number of digits are not known. I am looking for number of dashes between 1 and 3 between the numbers
texts = ['1-2-3-4-5','123-4567-8901','123-45-67-890','123-456728901','1234567890123','123--67890123','-234567890123','123456789012-','-23456789012-']
for text in texts:
print(text,re.findall(r"^\d+-?\d+-?\d+-?\d+$",text))
output:
1-2-3-4 ['1-2-3-4']
123-4567-8901 ['123-4567-8901']
123-45-67-890 ['123-45-67-890']
123-456728901 ['123-456728901']
1234567890123 ['1234567890123']
123--67890123 []
-234567890123 []
123456789012- []
-23456789012- []
I have been trying but without success
I need a regular expression for validating numbers that could contain dots and commas,
the number should be positive and there should be max of two numbers after the comma
Valid cases would be:
1000 - valid
1,000 - valid
1,000.22 - valid
-2 not valid
1,000.233 not valid
0 not valid
1.00,22 - not valid
Language is javascript
let valid =["1000","1,000","1,000.22"];
let notValid = ["-2","1,000.233 ","0","1.00,22"];
let rge = /^[1-9]+\d*(,\d{3})*(\.\d{1,2})?$/;
for(let x of valid)
console.log(x," è valida? ",rge.test(x));
for(let x of notValid)
console.log(x," è valida? ",rge.test(x));
Above there is a possible solution in Javascript, you haven't specified the language.
\d are numbers in the range [0-9]
The full stop . is a metacharacter (it means any character), to refer to the character . you have to escape it thereby \.
+ means at least 1 or more times
* means 0 or more times
? means 0 or 1 time
{1,2} means match minimum 1 time, maximum 2 times
The starting ^ and final $ refer to a exact matching otherwise you could have a partial matching of the string
A few assumptions:
Invalid: '123456789.12' and '12345,123.12'
I think the following does what you are after:
^[1-9](?:\d*|\d{0,2}(?:,\d{3})*(?:\.\d\d?)?)$
See the online demo
^ - Start-line anchor.
[1-9] - A single digit in the range 1-9.
(?: - Open a non-capture group:
\d* - 0+ Digits to allow any integer.
| - Or:
\d{0,2} - Between 0 to 2 digits;
(?:,\d{3})* - Followed by a non-capture group to allow any 0+ times a comma followed by 3 digits.
(?:\.\d\d?)? - Followed by an optional non-capture group to allow up to two decimals.
)$ - Close non-capture group and match the end-line anchor.
Or, if you also want to allow any integer followed by decimals (e.g: '123456789.01') you may change this to:
^[1-9](?:\d*|\d{0,2}(?:,\d{3})*)(?:\.\d\d?)?$
I think this regex should do the trick:
[1-9][\d,]*(\.\d{1,2})?
[1-9] - matches one character between 1 and 9 at the beginning (required to not match 0)
[\d,]* - matches zero or more digits or commas
(\.\d{1,2})? - zero or one group of a dot and one or two digits
For testing regexes I do recommend https://regex101.com/
I need a Regex to match the following:
After each 6 characters of a string there is a ';'
Examples:
aaaaaa;z5z5z5;zdzzzt; (Valid)
aaadzdaaa;z5z5dzdzz5;zdzdzd; (Not Valid)
I'v tried:
(([A-Za-z0-9]{6};$))
but it only validates according to the last sequence.
You should use
^(?:[A-Za-z0-9]{6};)*$
See regex demo
If there must be at least one sequence with a semi-colon, replace * with + quantifier:
^(?:[A-Za-z0-9]{6};)+$
You actually need both ^ start-of-string anchor and $ end-of-string anchor, and you should not have placed the $ anchor into the repeated group since there is only one end of string.
Here is the regex breakdown:
^ - start of string
(?:[A-Za-z0-9]{6};)* - 0 or more sequences of...
[A-Za-z0-9]{6} - exactly 6 ASCII letters or digits
; - a semi-colon
$ - end of string.
I would use:
^(?:\w{6};)*$
With:
^ assert position at start of a line
(?:\w{6};)* Non-capturing group
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\w{6} match any word character [a-zA-Z0-9_]
Quantifier: {6} Exactly 6 times
; matches the character ; literally
$ assert position at end of a line
I am trying to create a regex that only matches for valid dates (in MM/DD or MM/DD/YY(YY) format)
My current regex (\d+)/(\d+)/?(\d+)? is very simple but it matches any number that has a / before/after. I.e. if a string is 2015/2016 12/25 it will see both of these as matches but i only want the 12/25 portion.
Here is a link to some sample RegEx.
You can add word boundaries (\b) to make sure you match the date string as a whole "word" (so that the match does not start in the middle of a number) and restrict the occurrences \d matches with the help of limiting quantifiers:
\b(\d{2})/(\d{1,2})/?(\d{4}|\d{2})?\b
See the regex demo
The regex breakdown:
\b - word boundary to make sure there is a non-word character or start of string right before the digit
(\d{2}) - match exactly 2 digits
/ - match a literal /
(\d{1,2}) - match and capture 1 to 2 digits
/? - match 1 or 0 /
(\d{4}|\d{2})? - match 1 or 0 occurrences of either 4 or 2 digits
\b - trailing word boundary