简体   繁体   中英

My Regular Expression isn't picking up single digits

I wrote a regular expression for a leetcode problem and I am having issues with it. when I put the expression into regex101 all of the matches are correct except it won't match any singular digits such as the 2. when I change the \d+ at the end of it to \d* it accepts the 2, but it then accepts the 1e that I am trying to avoid. What is wrong here?

here is the regular expression:

^([\+-]{0,1}\d+[eE]{0,1}[\+-]{0,1}\d+)$

here is the text I am trying to match:

"2"
"0089"
"2e10"
"-90E3"
"3e+7"
"+6e-1"

here is the text I am trying to avoid matching:

"abc"
"1a"
"1e"
"e3"
"--6"
"-+3"
"95a54e53"

You have two d+ s, which means that you aren't able to match a string like "2" . You need to make the ability to specify the [eE][\+-]... part optional using the ?operator:

pattern = re.compile("^([\+-]{0,1}\d+([eE][\+-]?\d+)?)$")

Let's see if I can decompose your regular expression and it may help.


^([\+-]{0,1}\d+[eE]{0,1}[\+-]{0,1}\d+)$

Observation 1: ^(...)$ means that you do not want anything outside of your match to exist in the string you are matching against (aka, your pattern must include the whole string).


[\+-]{0,1}\d+[eE]{0,1}[\+-]{0,1}\d+

Observation 2: You have the duplicate pattern [\+-]{0,1}\d+ separated by [eE]{0,1} . Likely you intend to have a grouping in there somewhere; my first guess is that you want the e / E to be followed by digits, otherwise without digits 1e is a valid match. So something like [\+-]{0,1}\d+([eE][\+-]{0,1}\d+){0,1} would be more appropriate (group the letter with the following digits and have them all or none match).

Observation 3: you like to use {0,1} to specify that it matches 0 or 1 times, but there is a much more appropriate and convenient ? to do the same. The syntax you are using is more designed for the times when it is a much larger range or for the times that you want to match a higher minimum (while still having a maximum).


[\+-]{0,1}\d+

Observation 4: it looks like you are trying to match positive/negative numbers where the sign is optional for positive numbers. Google lead me quickly to this as an alternate example (though yours is a reasonable solution--I don't know the performance difference).


[eE]{0,1}

Observation 5: it looks like you are trying to find floating point numbers of a specific format ("mantissa" is the proper name). A quick Google search finds this that includes what you are going for.


EDIT: Conclusion (if my assumptions are right): ([-+]?\d+)([eE][-+]?\d+)?$ should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM