How to write this using regular expression? - regex

I am looking for a regex to match a string like this: 1,2,4-6,9,11-13,20.
Restrictions:
Only numbers, comma and hyphen are allowed
no spaces are allowed

Your question is rather vague. I would suggest improving it, or reading a tutorial on regexes.
Based on your restriction your regex is /^[-\d,]*$/ but I am quite sure that this is not what you want.
You should provide examples of input, output, the regex flavor you will be using and last but not least your attempts to solve the problem.

I am guessing you want to match comma seprated lists of positive integers or positive integer ranges. \d+ matches integers, to allow ranges, you'd use \d+(-\d+)?.
So, the regex
\d+(-\d+)?(,\d+(-\d+)?)*
would do.

Related

Regular Expression, testing for numbers

I'm just starting to learn regular expressions, and one of the questions was to match the different types of numbers. The ones that I needed to match are in below:
my regex: -?\d+,?\d+\.?e?\d+
3.14529
-255.34
128
1.9e10
123,340.00
however, from my regular expression, I failed to meet the first one and the fourth one. I saw the solution but I did not quite understand why it uses brackets. Can anyone explain? Thank you!
Your regex does not allow digits to follow a literal dot when you only have one digit preceding it. This is because you have twice a \d+ before matching the dot. In general, you have three mandatory \d+ in there, so you cannot match anything with less than three digits.
I would suggest this regex:
^-?\d{1,3}(,\d{3})*(\.\d+)?(e\d+)?$

Regex no two consecutive characters are the same

How do I write a regular expression where x is a string whose characters are either a, b, c but no two consecutive characters are the same
For example
abcacb is true
acbaac is false
^(?!.*(.)\1)[abc]+$ works if you follow the original question exactly. However, this does not work/check multiple "words" of characters a/b/c, ie. "abc cba".
The way it works is it asserts that any character is not followed by itself by utilizing a capture group inside a lookahead and that the entire string consists only of characters "a", "b", or "c".
Since the number of chars is limited, you can get away without a back reference in the look ahead:
^(?!.*(aa|bb|cc)[abc]*$
But I like tenub's answer better :)
using negative lookbehind: ^([abc]([abc](?<!(aa|bb|cc)))*)?$ TRY HERE
using negative lookahead: ^(((?!(aa|bb|cc))[abc])*[abc])?$ TRY HERE
Prefer either (both do the same job but differently) if you are going to use this regex as a part of some bigger regex that you might be creating.
In short, this is reusable. Copy & paste and it will do its work without disturbing any regex that is present around it.
In my humble opinion, regexes provided in #tenub and #Bohemian are not reusable which can cause bugs.
Note: empty string ("") will pass these 2 regexes. If you don't want it to, remove ? from regex.

How do regex positive look-behinds work?

I have been solving old question from stack so that I can improve my regex knowledge. As I have a basic knowledge of regex, most of them were easy but this question regex problem is tough.
It asks for a regex that extracts from this kind of string ou=persons,ou=(.*),dc=company,dc=org the last string immediately preceded by a comma not followed by (.*). In the last case, this should give dc=company,dc=org.
The solution is (?<=,(?!.*\Q(.*)\E)).* but I cannot understand its flow. I understood (?!.*\Q(.*)\E) portion but other are still mystery to me. Specially ?<= which is a positive look-behind. Does it search from end of string? Can anyone explain it to me like I am a 7 year old kid — and please http://regex101.com/ is not helping.
The RegEx (?<=,(?!.*\Q(.*)\E)).* look-behind potion works like this:
Start at the beginning of the string at first character.
Can we match the the thing we are looking for? ,(?!.*\Q(.*)\E)
If we can't: Move forward one character, Go To 2. and check match again.
If a match is found: Capture all the remaining characters until we can't find any .* (or generally then try the matching the remaining RegEx).
For a more wordly explaination consider reading Lookahead and Lookbehind Zero-Length Assertions.
A lookbehind allows you to specify a context just before the actual match.
You can say ,(dc=) and only return the capture group, or ,\Kdc=, or (?<=,)dc= to return the match on dc= but require that the comma is present just before the match.
The facility also allows for multiple lookbehinds, so you could do (?<=a.*)(?<=b.*)c to match c only if it is preceded by both a and b somewhere in the input.
A lookbehind is basically syntactic sugar, in that you can usually rephrase your conditions using some other regex construct. It can be really handy when you have multiple unanchored constraints, like in the last example

Regex expression - one or multiple strings seperated by comma

Novice regex question here.
I need a regex that will accept one or more of the following strings. If there is multiple strings, they need to be separated by a comma.
foo
bar
Any help or a point in the right direction would be appreciated.
^(foo|bar)(,(foo|bar))*$
does that. The capturing groups are not necessary, you could also write this (slightly more efficient) with non-capturing groups as
^(?:foo|bar)(?:,(?:foo|bar))*$
To avoid repeats, you can use a negative lookahead assertion:
^(foo|bar)(?:,(?!\1)(?:foo|bar))?$
(Notice the ? instead of * - if only a single repetition is possible, this makes more sense.)
This approach quickly becomes complicated when a higher number of strings is to be checked. While it's theoretically possible to do that with a regex as well, it's probably not a good idea.

Regex to find special characters in a String with some exceptions

I just had a similar (but not exact) question answered. Now I need help with the question mentioned below.
I want to write a regex which matches a character if its a non word, non digit and non star (*) character. So, the characters [0-9][a-z][A-Z] * should not match and the others should.
I tried writing [\W[^*]] but it doesn't seem to work.
Try this instead:
[^\w\*]
The simplest regular expression that matches a single character, that is not one of those you described, independent of any particular regular-expression extensions, would be:
[^0-9a-zA-Z *]
[^\w\*]
Simple enough.
Please try the following regex:
[\W_]