Perl Regex -?\d+(?:\.\d+)? [duplicate] - regex

This question already has answers here:
What is a non-capturing group in regular expressions?
(18 answers)
Closed 6 years ago.
This regex is supposed to match any numbers (real or integer - no scientific notation). However, I am not sure what is the use of '?:' inside the parentheses.
Could anyone explain this along with some examples? Thank you very much.

In the regex
?\d+(?:\.\d+)?
The ?: quantity inside the group in parenthesis instructs the regex engine to not capture the group, which it otherwise would.
By not capturing the quantity in parenthesis, the capture group available (which should be the first one, and the entire expression) would just be the digits occurring before the decimal point, should the number have a fractional component.

Related

Finding Repeated Patterns using a Regular Expression [duplicate]

This question already has answers here:
How to find overlapping matches with a regexp?
(4 answers)
Closed 3 years ago.
I'm trying to extract the repeated pattern from a string.
For example with something like "112112112112" I would want to end up with "112".
I've been having problems where I either end up with "1" or "112112".
The patterns can be of any size.
Here's an example of the kind of expressions I've been playing around with.
^(.+)(?=\1)
There are repeated patterns with different sizes, if 3 would be desired, for instance, we'd use a quantifier for that, such as:
(.{3})(?=\1)
Demo 1
or
(.{3,5})(?=\1)
Demo 2

RegEx for special characters and decimals except for commas [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
Regex testing for special characters, decimal except for hyphen, commas, alpha-numeric.
Attempt
^(\+|-)?([0-9]+)$/
I'm trying to write a regex to match special characters, decimal except for commas, a hyphen, alpha-numeric.
Welcome!
Maybe, you might just want to list the chars that you wish in a [] and test it. Maybe, a simple expression like this would be desired for you and you could work on it:
^([?!"'~&%$*##0-^#9]+)$
You might want to use this tool and design an expression that you wish, then test it with real samples and maybe change it as you wish.
You can also use an online visualizer to view how your expression would work:

regexp don't match if contain specific digit length [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 5 years ago.
I've tried to make a regular expression that match anything except if contains a 11 digits like 12345678910 so don't match anything
what i have tried
[^\d{11}]
but {11} doesn't work with \d expression
so what i have to do ?
you can use the regex
^(?!.*\d{11}).*$
see the regex101 demo
It's not a very good task for regex to solve actually, because you have to describe every string that doesn't contain 11 consecutive digits.
If possible, I suggest matching a string that does contain 11 consecutive digits, then inverting the success of that match with the language or tool from which you execute this regex.
Depending on your regex flavour, you might also be able to use a negative lookahead such as presented in other answers.
This seemed to work for me using a negative look around:
/^((?!\d{11}).)*$/gm

Finding a group match part of another group in a regex [duplicate]

This question already has answers here:
How to use regex to find all overlapping matches
(5 answers)
Closed 5 years ago.
I was just wondering how to match a group which has characters already in another group.
If we take this string for example: "aba" and want to match every group of (ab) or (ba).
Obviously (ab|ba) would work, my only problem with that is it only catches one group which is aba but i also want to capture aba, do I have to use a more complex regex for this case?
You can easily achieve this using this regex
(?=(ab|ba))
It will match all the occurrences of both 'ba' and 'ab' even the overlap ones.

reg expression of number of duplicated numbers [duplicate]

This question already has an answer here:
Split regex to extract Strings of contiguous characters
(1 answer)
Closed 8 years ago.
I have following question need reg expert help. I have a string "11122233344456", I need put same digits into a string. For above example, it shall be "111","222","333","444","5","6".
Another example: "223334456111", it shall be "22","333","44","5","6","111".
Would some regex expert help me to find the solution?
You can use this regex:
((\d)\2*)
And grab captured group #1
RegEx Demo