I current have this regex:
/^\+?\d+(\d|\-)+\d+$/
this accepts
12345
123-456
+12345
+12345-12345
my problem that this also accepts
123--123
123-------3242-324324
How can I fix the regex to not accept consecutive dash in between numbers?
This will be correct one
^\+?\d+(-\d+)*$
Regex Demo
or modifying a bit of your regex with negative lookahead will also work
^(?!.*--)\+?\d+(\d|\-)+\d+$
Regex Demo
Related
I have set of strings which looks like the below. Each string has 3 numbers separated with an underscore (_). Each number is a value between 1 - 100.
ma_1_1_1
ma_2_100_59
ma_29_29_29
ma_100_100_100
ma_7_72_78
ma_10_10_100
ma_4_4_49
I want to write a regular expression where I can get the strings whose digits are all same. For example my output would be
ma_1_1_1, ma_29_29_29 and ma_100_100_100
Like this?
^ma_(\d+)_\1_\1$
See a demo on regex101.com.
This uses backreferences with the first captured group as well as anchors.
Use back-references to make a regex match a previous group again:
ma_(100|[1-9][0-9]?)_\1_\1\b
Regex101 Demo
This will also validate that the numbers are within range. If this validation is unnecessary, use (\d+) for the capture group.
This answer is a modification to #4castle which will only extract the strings with similar numbers.
grep("ma_(100|[0-9][0-9]|[0-9])(_\\1)(_\\1)\\b", stringList, value = T)
I have the following sentence: total 10 item(s) 26,50
I want to extract the number 26,50 based on the word "total". I came this far with a Positive Lookbehind but I'm stuck now. (?<=total )(.*)(?=\d)
You don't need lookbehind. Use groups:
https://regex101.com/r/oC0dM3/2
total\s+(?P<COUNT>\d+)\s+item(?:\(s\))?\s+(?P<PRICE>\d+(?:,\d+)?)
Many Regex engine does not support variable variable length Look behind, in those cases your Regex would be pretty inefficient if you use lookbehind.
Use pattern grouping instead:
^total[^)]+\)\s+(.*)$
The only captured group here is your desired portion.
^total[^)]+\)\s+ matches upto the last whitespace before the desired pattern
(.*)$ gets our desired portion
Demo
I need regex to return invalid on a match. Specifically, the match is a string that starts with an A or an M and is followed by four numbers ie, A1223. The four numbers could be any random sequence.
I'm sure lookarounds are the way to handle this but I haven't grasped regex as a concept just yet. Thus far I've discovered how to capture the matched strings separate from other strings with the following.
([\s\S]*?)(A[\d][\d][\d][\d]|M[\d][\d][\d][\d])
Appreciate the help.
Regex doesn't really have match negation, but you can (ab)use a negative lookahead assertion to do inverted matching:
^((?!\s[AM]\d{4}).){6}
to match all strings not starting with A or M followed by 4 digits:
with negative lookahead:
^(?![AM]\d{4}).*
with consuming pattern using () capture groups:
[AM]\d{4}.*|(.+)
I m looking for regex where it accepts alphanumeric characters and alphabets
Example
RAM123 -positive
ram123 - positive
rAm123-positive
Ram -positive
ram - positive
123-negative
I have tried using [0-9a-zA-Z].* but it also accepts numeric characters..which should not be..thr regex should accept any alphanumeric/ alphabets other than nums and special charcters
You need to use a negative lookahead assertion at the start.
^(?!\d+$)[\da-zA-Z]+$
DEMO
^[\da-zA-Z]*[a-zA-Z][\da-zA-Z]*$
You can simply use this.See demo.
https://regex101.com/r/nS2lT4/17
I need some help with a RegEx pattern match.
How do i write a regex if i want it to match
N-NN-N-NN-NN-N-NNN
but also
N-NN-NN-NN
Exmaple:
10pcs- ratchet spanner combination wrench 6-8-10-11-12-13-14-15-17-19
Cr-v,heated 12pcs-1/4dr 4-4.5-5-5.5-6-7-8-9-10-11-12-13 Cr-v,heated
17pcs-1/2dr 10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-27-30
Cr-v,heated 1-2-33 Cr-V heater 1-.2-1-4
It needs to match where they is at least 2 - in the total string. So a phone number like this 020-11223344 is not to be matched.
The strings almost always look like this 6-8-10-11-12-13-14-15-17-19 , except sometimes a . can apper before a number, they also differ in length, is it possible?
I came up with this so far but it also matches on phone numbers and when a . appears it doenst match at all.
(\d-[^>])
On this page you can find the different patters: http://www.cazoom.nl/en/partij-aanbod/186-pcs-working-tools-trolly-3
What about this pattern:
[\d.]+(?:-[\d.]+){2,}
Match [\d.]+ if followed by at least 2x -[\d.]+
(?: Using a non capturing group for repetition.
test at regex101
The following regex will match the thing.
(?:\.?\d\.?\d?-){2,}\.?\d\.?\d?
Debuggex Demo
Just try with following regex:
^\d-\d{2}-\d(\d-\d{2})|(\d-\d{2}-\d-\d{3})$