python - Regular Expression for pattern surrounded by optional angle brackets -


I'm alternatively trying to use regular expressions to find the number surrounded by angle brackets, i.e. < Code> & lt; & Gt; . The method I am using,

  & lt ;? (? P & lt; degrees & gt; [- +]? [- + 0-9EE] +) & gt;  

You can find the surrounding cell. But if only one of the brackets, then it also matches. I only want to match it if both are open and close angle brackets or none of them.

I want to match things like this:

  - 11.88  

And I do not want a match:

  & lt; 1.23 & lt; -abcdefg & gt;  

I also do not want to repeat the pattern used to match the number. It feels like an error-prone solution.

If you do not want to, you can probably use the named conditional Repex number to repeat For:

  ^ [- +]? (? P & lt; o & gt; & lt;)? [- 0-9.EE] + (? (O) & gt; | $$  

Breakdown:

  ^ # Start [- +] ? # Get one - / + if someone (? P  


If you want something to actually verify the number (and to prevent content like - <2>, ), you Can use something like this Are:

  ^ (? <>>  gt; & lt;) ((??) | [ - +]) \ d + (?. \ \ D + ([E] [- +] \ d + (?: \. \ D +)?)?)? (? (? O)> | $ $  

Note: I do not generally accept things like .2e-3 (i.e. decimal without zero) therefore also prevents the above. If the string is 0.2e-0.3 , but allows both leading and indexed zeros.


Comments