So as an exercise I wanted to match any JS number. This is the one I could come up with:
/^(-?)(0|([1-9]\d*?|0)(\.\d+)?)$/
This however doesn't match the new syntax with underscore separators (1_2.3_4). I tried a couple of things but I couldn't come up with something that would work. How could I express all JS numbers in one regex?
For the format in the question, you could use:
^-?\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?$
See a regex demo.
Or allowing only a single leading zero:
^-?(?:0|[1-9]\d*)(?:_\d+)*(?:\.\d+(?:_\d+)*)?$
The pattern matches:
^ Start of string
-? Match an optional -
(?:0|[1-9]\d*) Match either 0 or 1-9 and optional digits
(?:_\d+)* Optionally repeat matching _ and 1+ digits
(?: Non capture group
\.\d+(?:_\d+)* Match . and 1+ digits and optionally repeat matching _ and 1+ digits
)? Close non capture group
$ End of string
See another regex demo.
how about this?
^(-?)(0|([1-9]*?(\_)?(\d)|0|)(\.\d+)?(\_)?(\d))$
Related
I'm having an issue and I'm hoping there is someone who is more knowledgeable with Regex that can help me out.
I'm trying to extract data from a PDF file which contains a budget line items. I'm using this regex pattern to get the index of the first number so I can then extract the numbers to the right.
Regex pattern:
(([(]?[0-9]+[)]? )|([(]?[0-9]+[)]?)|(- )|(-))+$
Line item: 'Modernization and improvement (note 9) 260 (180) 640 - 155'
This works well for 99% of the line items except this one I came across. The problem is the pattern matches the '9)' in what is the text portion.
Is there any way with this Regex pattern to say if there are brackets, the inside must contain numbers only?
Thanks!
You can repeat all possible options until the end of the string:
(?:\(\d+\)|\d+(?:\s*-\s*\d+)?)(?:\s+(?:\(\d+\)|\d+(?:\s*-\s*\d+)?))*$
Explanation
(?: Non capture group
\(\d+\) Match 1+ digits between parenthesis
| Or
\d+(?:\s*-\s*\d+)? Match 1+ digits and optionally match - and 1+ digits
) Close the non capture group
(?: Non capture group to repeat as a whole part
\s+ Match 1+ whitespace chars
(?:\(\d+\)|\d+(?:\s*-\s*\d+)?) Same as the first pattern
)* Close the non capture group and optionally repeat it
$ End of string
Regex demo
I'm trying to write a regex for valid websocket address but I couldn't figure out what's wrong. Here's what I've already tried
/(^ws:\/\/|^wss:\/\/)(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:)|([a-zA-Z]+))([0-9]{1,5})/gm
Regex should match below
ws://0.0.0.0:8080
wss://192.168.0.25:12345
ws://localhost:3333
You may use
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[a-zA-Z]+):([0-9]{1,5})$/
See the regex demo
Note that [a-zA-Z]+ won't be enough as it only matches ASCII letters.
Use [^\/]+ instead if you want to match any chars but /:
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[^\/]+):([0-9]{1,5})$/
See this regex demo.
To just match FQDNs, use
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|(?=[^\/]{1,254}:[0-9]{1,5}$)(?:(?=[a-zA-Z0-9-]{1,63}\.)(?:xn--+)?[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,63}):([0-9]{1,5})$/
Adaped from this solution by Tim Pietzcker. There are all the explanations in his post. See this regex demo.
The main problem was with :: it was only matched after numeric IP pattern, not after the [a-zA-Z]+.
Details
^ - start of string
(wss?:\/\/) - ws:// or wss://
([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[a-zA-Z]+) - 1 to 3 digits followed with 3 occurrences of . and 1 to 3 digits or 1+ letters (or, if [^\/]+ is used, 1+ chars other than /)
: - :
([0-9]{1,5}) - 1 to 5 digits
$ - end of string.
The second alternation does not match the :
You could move it outside of the alternation to match it for both of the options.
If you don't need the capturing groups, you could omit them.
The first alternation could be shortened making the extra s optional:
^(wss?:\/\/)(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|([a-zA-Z]+)):[0-9]{1,5}$
Regex demo
I've read other stackoverflow posts about a simple arithmetic expression regex, but none of them is working with my issue:
I need to validate this kind of expression: "12+5.6-3.51-1.06",
I tried
const mathre = /(\d+(.)?\d*)([+-])?(\d+(.)?\d*)*/;
console.log("12+5.6-3.51-1.06".match(mathre));
but the result is '12+5', and I can't figure why ?
You only get 12.5 as a match, as there is not /g global flag, but if you would enable the global flag it will give partial matches as there are no anchors ^ and $ in the pattern validating the whole string.
The [+-] is only matched once, which should be repeated to match it multiple times.
Currently the pattern will match 1+2+3 but it will also match 1a1+2b2 as the dot is not escaped and can match any character (use \. to match it literally).
For starting with digits and optional decimal parts and repeating 1 or more times a + or -:
^\d+(?:\.\d+)?(?:[-+]\d+(?:\.\d+)?)+$
Regex demo
If the values can start with optional plus and minus and can also be decimals without leading digits:
^[+-]?\d*\.?\d+(?:[-+][+-]?\d*\.?\d+)+$
^ Start of string
[+-]? Optional + or -
\d*\.\d+ Match *+ digits with optional . and 1+ digits
(?: Non capture group
[-+] Match a + or -
[+-]?\d*\.\d+ Match an optional + or - 0+ digits and optional . and 1+ digits
)+ Close the noncapture group and repeat 1+ times to match at least a single + or -
$ End of string
Regex demo
You would try to use this solution for PCRE compatible RegExp engine:
^(?:(-?\d+(?:[\.,]{1}\d)?)[+-]?)*(?1)$
^ Start of String
(?: Non capture group ng1
(-?\d+(?:[\.,]{1}\d)?) Pattern for digit with or without start
"-" and with "." or "," in the middle, matches 1 or 1.1 or 1,1
(Matching group 1)
[+-]? Pattern for "+" or "-"
)* Says
that group ng1 might to repeat 0 or more times
(?1) Says that
it must be a digit in the end of pattern by reference to the first subpattern
$ End of string
As JS does not support recursive reference, you may use full version instead:
/^(?:(-?\d+(?:[\.,]{1}\d)?)[+-]?)*(-?\d+(?:[\.,]{1}\d)?)$/gm
I have a regex expression like this
/^[0-9]*\s*kg\s*[0-9]*$/
This accepts strings like 100 kg or 100kg. I am tring to make it also accept 100.1kg or 100.1 kg or 10,20 kg and so on (both dot and decimal should be accepted). How do I change that current regex so that those numbers I mentioned also tests true? I have seen this regex which supports numbres and decimals but I am unsure how to combine it with my regex.
^[0-9]{1,2}([,.][0-9]{1,2})?$
To make it optional you could use an optional non capturing group (?: and add matching 0+ times a whitespace after it:
^[0-9]+(?:[,.][0-9]+)?\s*kg$
Regex demo
Now it will match
^ Start of the string
[0-9]+ 1+ digits
(?: Non capturing group
[,.][0-9]+ match comma or dot and 1+ digits
)? Close non capturing group and make it optional
\s*kg Match 0+ times a whitespace character
$ End of the string
For incorporating decimal values optionally in addition to whole number values, you can just place this regex (?:[.,]\d+)? after ^[0-9]* which will allow it to support decimal values like you mentioned in your post. Overall regex becomes this,
^\d*(?:[.,]\d+)?\s*kg\s*$
Demo
I'm stuck trying to capture a structure like this:
1:1 wefeff qwefejä qwefjk
dfjdf 10:2 jdskjdksdjö
12:1 qwe qwe: qwertyå
I would want to match everything between the digits, followed by a colon, followed by another set of digits. So the expected output would be:
match 1 = 1:1 wefeff qwefejä qwefjk dfjdf
match 2 = 10:2 jdskjdksdjö
match 3 = 12:1 qwe qwe: qwertyå
Here's what I have tried:
\d+\:\d+.+
But that fails if there are word characters spanning two lines.
I'm using a javascript based regex engine.
You may use a regex based on a tempered greedy token:
/\d+:\d+(?:(?!\d+:\d)[\s\S])*/g
The \d+:\d+ part will match one or more digits, a colon, one or more digits and (?:(?!\d+:\d)[\s\S])* will match any char, zero or more occurrences, that do not start a sequence of one or more digits followed with a colon and a digit. See this regex demo.
As the tempered greedy token is a resource consuming construct, you can unroll it into a more efficient pattern like
/\d+:\d+\D*(?:\d(?!\d*:\d)\D*)*/g
See another regex demo.
Now, the () is turned into a pattern that matches strings linearly:
\D* - 0+ non-digit symbols
(?: - start of a non-capturing group matching zero or more sequences of:
\d - a digit that is...
(?!\d*:\d) - not followed with 0+ digits, : and a digit
\D* - 0+ non-digit symbols
)* - end of the non-capturing group.
you can use or not the ñ-Ñ, but you should be ok this way
\d+?:\d+? [a-zñA-ZÑ ]*
Edited:
If you want to include the break lines, you can add the \n or \r to the set,
\d+?:\d+? [a-zñA-ZÑ\n ]*
\d+?:\d+? [a-zñA-ZÑ\r ]*
Give it a try ! also tested in https://regex101.com/
for more chars:
^[a-zA-Z0-9!##\$%\^\&*)(+=._-]+$