Regular expression to match floats only [duplicate] - regex

This question already has answers here:
Matching numbers with regular expressions — only digits and commas
(10 answers)
regular expression for finding decimal/float numbers?
(9 answers)
Closed 7 years ago.
I need to do a regular expression to match the floats only, what i got is the following :
[\-\+]?[0-9]*(\.[0-9]+)?
But this match also the below
123123132 ,
05/03/1994
I only need want to match the number with the decimal point

Your regex is almost correct for your purpose.
It finds 123123132, because the last part is optional. Removing the ? solves that.
[-+]?[0-9]*(\.[0-9]+)
With that adjustment, it might still find matches in strings like .12/39/3239, if you don't want that to happen, insert enforce matching over the complete string by inserting ^ and $:
^[-+]?[0-9]*(\.[0-9]+)$

How about:
([+-]?[0-9]*\.[0-9]*)
You can see it working here

Here is a regexp handling also existing exponents:
[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?
Debuggex Demo
Additionally you should force the hole string to be matched to avoid matchings within your date values.
^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
By the way here is a nice tutorial about matching floating point numbers using regular expressions: http://www.regular-expressions.info/floatingpoint.html.

Related

Regex to match only if symbol is found at most once in fixed length pattern [duplicate]

This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 3 years ago.
I need help with a regex that should match a fixed length pattern.
For example, the following regex allows for at most 1 ( and 1 ) in the matched pattern:
([^)(]*\(?[^)(]*\)?[^)(]*)
However I can not / do not want to use this solution because of the *, as the text I have to scan through is very large using it seems to really affect the performance.
I thus want to impose a match length limit, e.g. using {10,100} for example.
In other words, the regex should only match if
there are between 0 and 1 set of parenthese inside the string
the total length of the match is fixed, e.g. not infinite (No *!)
This seems to be a solution to my problem, however I do not get it to work and I have trouble understanding it.
I tried to use the accepted answer and created this:
^(?=[^()]{5,10}$)[^()]*(?:[()][^()]*){0,2}$
which does not seem to really work as expected: https://regex101.com/r/XUiJZz/1
Also please do not mark this question a duplicate of another question, if the answers in that question make use of the kleene star operator, it wont help me.
Edit:
I know this is a possible solution, but I'm wondering if there is a better way to do it:
([^)(]{0,100}\(?[^)(]{0,100}\)?[^)(]{0,100})
I thus want to impose a match length limit, e.g. using {10,100}
You may want to anchors add a lookahead assertion in your regex:
^(?=.{10,100})[^)(]*(?:\(?[^)(]*\))?[^)(]*$
(?=.{10,100}) is lookahead condition to assert that length of string must be between 10 and 100.
RegEx Demo

build a string that excludes text containing two specific words [duplicate]

This question already has answers here:
Regular expression to match strings that do NOT contain all specified elements
(2 answers)
Closed 2 years ago.
I am trying to find the inverse of the following string but with no success:
(?i)(?s)^(?=.*?word1)(?=.*?word2)
I built this but it is for one word only and it does not even function properly, considering that if I test it with regex101 I got one match while I should get no match:
(?i)(?s)(?!.*?word2)^.*$
Please see the following link: https://regex101.com/r/qS7yN9/72
Hope you guys can help to build the right string.
You can effectively negate the result by wrapping a negative look-ahead assertion around the whole thing:
(?i)(?s)^(?!(?=.*?word1)(?=.*?word2))

Find the shortest match that matches a specific condition using regex [duplicate]

This question already has answers here:
Regular expressions: Ensuring b doesn't come between a and c
(4 answers)
Closed 4 years ago.
I'd like to find these three strings in any order and the result may have all these three strings including any character between them with the shortest length.
strings are: "ACT", "AGT" and "CGT".
Sample input: "ACTACGTTTAGTAACTCGTCT"
I tried but the regex returns the first occurrence matched which is "ACTACGTTTAGTAACTCGT"
/(ACT.*AGT.*CGT)|(ACT.*CGT.*AGT)|(AGT.*ACT.*CGT)|(AGT.*CGT.*ACT)|(CGT.*ACT.*AGT)|(CGT.*AGT.*ACT)/g
Output has to be "AGTACTCGT"
You can't return separate bits of a string already concatenated in one go.
See here: Regular expression to skip character in capture group
You can first match each bit, using parentheses to group them, and then put them together in a separate step

Regex negative range algorithm [duplicate]

This question already has an answer here:
How to implement regular expression NFA with character ranges?
(1 answer)
Closed 7 years ago.
I need to implement positive and negative ranges in my regex matcher.
It looks not difficult for positive range:
[1-3] == (1|2|3)
But I do not understand how to convert negative range [^1-3] to simple regex string.
Is it possible?
Thanks!
Update
Not. Seems it is impossible.
Ok, how regex libraries process negative ranges in this case?
If the regex engine you're using supports negative lookahead, you can do it like this:
(?!1|2|3).
?! is the negative lookahead operator. It says "the characters that follow this expression must not match this expression." It makes a negative match without advancing the cursor. Here it's followed by a . to indicate any character.

My regular expression matches too much. How can I tell it to match the smallest possible pattern? [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I have this RegEx:
('.+')
It has to match character literals like in C. For example, if I have 'a' b 'a' it should match the a's and the ''s around them.
However, it also matches the b also (it should not), probably because it is, strictly speaking, also between ''s.
Here is a screenshot of how it goes wrong (I use this for syntax highlighting):
I'm fairly new to regular expressions. How can I tell the regex not to match this?
It is being greedy and matching the first apostrophe and the last one and everything in between.
This should match anything that isn't an apostrophe.
('[^']+')
Another alternative is to try non-greedy matches.
('.+?')
Have you tried a non-greedy version, e.g. ('.+?')?
There are usually two modes of matching (or two sets of quantifiers), maximal (greedy) and minimal (non-greedy). The first will result in the longest possible match, the latter in the shortest. You can read about it (although in perl context) in the Perl Cookbook (Section 6.15).
Try:
('[^']+')
The ^ means include every character except the ones in the square brackets. This way, it won't match 'a' b 'a' because there's a ' in between, so instead it'll give both instances of 'a'
You need to escape the qutoes:
\'[^\']+\'
Edit: Hmm, we'll I suppose this answer depends on what lang/system you're using.