I have implemented the following Regex pattern
^[\d,|+\d,]+$
It validates the following pattern
14,+96,4,++67
I need to invalidate ++67 from my pattern and I need to keep values with only a single leading + sign.
How should I change my Regex pattern?
You may use
^\+?\d+(?:,\+?\d+)*$
See the regex demo.
Details
^ - start of string
\+? - an optional + char
\d+ - 1+ digits
(?:,\+?\d+)* - zero or more repetitions of a sequence of patterns:
, - a comma
\+? - an optional plus
\d+ - 1+ digits
$ - end of string
Perhaps you meant to do this?
^(\d,|\+\d,)+$
Square brackets use every character or character class within, which does not appear to be what you really want. For disjunction you need round brackets.
You can try this one
^(\d+\,?|\+\d+,?)+$
Related
I am attempting to pick apart data from the following string utlizing a regex expression:
Ethane, C2 11.7310 3.1530 13.9982 HV, Dry # Base P,T 1432.00
The ultimate goal is to be able to pull out the middle three data points as individual values 11.7310, 3.153, 13.9982
The code expression I am working with at the moment is as follows:
(?<=C2 )(\d*\.?\d+)
This yields a full match of 11.7310 and a Group 1 match of 11.7310, but I can't figure out how to match the other two data points.
I am using PCRE (PHP) to create my expression.
You may use
(?:\G(?!^)|\bC2)\s+\K\d*\.?\d+
See the regex demo.
Details
(?:\G(?!^)|\bC2) - either the end of the previous successful match or C2 whole word
\s+ - 1+ whitespaces
\K - match reset operator discarding all the text matched so far in the match memory buffer
\d* - 0+ digits
\.? - an optional dot
\d+ - 1+ digits.
Given any URL, like:
https://stackoverflow.com/v1/summary/1243PQ/details/P1/9981
How do I extract the numeric or alphanumeric part of the URL? I.e. the following strings from the url given above:
1. v1
2. 1243PQ
3. P1
4. 9981
To rephrase, a regex to extract strings from a string (URL) which have at least 1 digit and 0 or more alphabet characters, separated by '/'.
I tried to capture a repeating group (^[a-zA-Z0-9]+)+ and ([a-zA-Z]{0,100}[0-9]{1,100})+ but it didn't work. In hindsight intuition does say this shouldn't work. I am unsure how do I match patterns over a group and not just a single character.
If I understand what you really want:
Extracting parts with only numbers or with numbers following alphabets
then; I can suggest this regex:
\b[a-zA-Z]*[0-9]+[a-zA-z]*\b
Regex Demo
I use \b to assert position of a word boundary or a part.
As numbers are required and alphabets can comes before or after that I use above regex.
If following alphabets are not required then I can suggest this regex:
\b[a-zA-z0-9]*[0-9]+[a-zA-Z0-9]*\b
Regex Demo
I believe this should work for you:
(\d*\w+\d+\w*)
EDIT: actually, this should be sufficient
(\w+\d+\w*)
or
(\w*\d+\w*)
Well, you could do this:
(\w*\d+\w*) with the g (global) regex option
On the example URL, it would look like this:
const regex = /(\w*\d+\w*)/g;
const url = 'https://stackoverflow.com/v1/summary/1243PQ/details/P1/9981';
console.log(url.match(regex))
Try \/[a-zA-Z]*\d+[a-zA-Z0-9]*
Explanation:
\/ - match / literally
[a-zA-Z]* - 0+ letters
\d+ - 1+ digits - thanks to this, we require at least one digits
[a-zA-Z0-9]* - 0+ letters or digits
Demo
It will captrure together with / at the beginning, so you need to trim it.
I have a numbers operation like this:
-2-28*95+874-1545*-5+36
I need to extract operands, not implied in a multiplication operation with a regex:
-2
+874
+36
I tried things like that without success:
[\+,-]\d+(?=\+|-|$)
This regex matches -5, too, and
(?(?=\d+)[\+,-]|^)\d+(?=\+|-|$)
matches nothing.
How do I solve this problem?
You may use
(?<!\*)[-+]\d*\.?\d+(?![*\d])
See the regex demo
Details
(?<!\*) - (a negative lookbehind making sure the current position is) not immediately preced with a * char
[-+] - - or +
\d* - 0 or more digits
\.? - an optional . char
\d+ - 1+ digits
(?![*\d]) - not immediately followed with a * or digit char.
See the regex graph:
This RegEx might help you to capture your undesired pattern in one group (), then it would leave your desired output:
(((-|\+|)\d+\*(-|\+|)\d+))
You can also use other language specific functions such as (*SKIP)(*FAIL) or (*SKIP)(*F) and get the desired output:
((((-|\+|)\d+\*(-|\+|)\d+))(*SKIP)(*FAIL)|([s\S]))
You can also DRY your expression, if you wish, and remove unnecessary groups that you may not need.
Another option could be to match what you don't want and capture in a group what you want to keep. Your values are then in the first capturing group:
[+-]?\d+(?:\*[+-]?\d+)+|([+-]?\d+)
Explanation
[+-]?\d+ Optional + or - followed by 1+ digits
(?:\*[+-]?\d+)+ Repeat the previous pattern 1+ times with an * prepended
| Or
([+-]?\d+) Capture in group 1 matching an optional + or - and 1+ digits
Regex demo
I want to write a regex pattern to match a string starting with "Z" and not containing the next 2 characters as "IU" followed by any other characters.
I am using this pattern but it is not working Z[^(IU)]+.*$
ZISADR - should match
ZIUSADR - should not match
ZDDDDR - should match
Try this regex:
^Z(?:I[^U]|[^I]).*$
Click for Demo
Explanation:
^ - asserts the start of the line
Z - matches Z
I[^U] - matches I followed by any character that is not a U
| - OR
[^I] - matches any character that is not a I
.* - matches 0+ occurrences of any character that is not a new line
$ - asserts the end of the line
When you want to negate certain characters in a string, you can use character class but when you want to negate more than one character in a particular sequence, you need to use negative look ahead and write your regex like this,
^Z(?!IU).*$
Demo
Also note, your first word ZISADR will match as Z is not followed by IU
Your regex, Z[^(IU)]+.*$ will match the starting with Z and [^(IU)]+ character class will match any character other than ( I U and ) one or more times further followed by .* means it will match any characters zero or more times which is not the behavior you wanted.
Edit: To provide a solution without look ahead
A non-lookahead based solution would be to use this regex,
^Z(?:I[^U]|[^I]U|[^I][^U]).*$
This regex has three main alternations which incorporate all cases needed to cover.
I[^U] - Ensures if second character is I then third shouldn't be U
[^I]U - Ensures if third character is U then second shouldn't be I
[^I][^U] - Ensures that both second and third characters shouldn't be I and U altogether.
Demo non-look ahead based solution
I broke it down to two, but I'm wondering if it's possible in one.
My two regex
/^[^\s+ ]+$/
/(.*[a-zA-Z].*)/
You can use
/^[^+\s]*[a-z][^+\s]*$/i
See the regex demo
The pattern matches:
^ - start of string
[^+\s]* - zero or more characters other than + and whitespace
[a-z] - a letter (case insensitive - see /i modifier)
[^+\s]* - zero or more characters other than + and whitespace
$ - end of string
This expressions only requires one letter, and there can be any number of characters other than a space and a plus on both sides of the letter.
Try this. I'm not sure what you mean by "unique", though:
/^[^+\s]*[A-Za-z][^+\s]*$/
Why not both?
^(?=.*[a-zA-Z])[^\s+]+$
Uses lookahead.
^(?=.*[a-zA-Z])[^\s+]+$
^ start of regex
(?=.*[a-zA-Z]) make sure there is at least a letter ahead
[^\s+]+ make every character is not a plus or any whitespace character
$ end of regex
Notice how I changed your [^\s+ ] into my [^\s+] because \s already included the space (U+0020).