Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to get an intervalle with a '/'

I have this Descriptions

Description
T2 SMD MOS N/P SI1555 SOT363 700/600mA 20/8V -55/150C Dual 390/850mOHM
T2 SMD MOS N/P FDS8858CZ SO-8 8.6/7.3A 30V 0.017OHM
T2 SMD MOS N/P EM6M2T2R SOT563 200/200mA 20V 1OHM ESD PROT
FLTR SMD 0603 FER BEAD 220OHM @100MHZ +/-25% 0,4OHM 1.4A

what i want to return is :

Current
700/600mA
8.6/7.3A
200/200mA
1.4A

For that i tried this regex

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(\s|^)(-)?(\d+\.?\d*(A|mA)(DC|AC)?)(?=\s|$)

but could not get work with values contained by a / as 700/600mA , 8.6/7.3A , 200/200mA

How could i correct my regex

>Solution :

You can use

(?<!\S)-?\d+(?:\.\d+)?(?:/-?\d+(?:\.\d+)?)?m?A(?:[DA]C)?(?!\S)

Explanation

  • (?<!\S) Assert a whitespace boundary to the left
  • -?\d+(?:\.\d+)? Match 1+ digits with an optional decimal part
  • (?:/-?\d+(?:\.\d+)?)? Optionally match / and 1+ digits with an optional decimal part
  • m?A Match an optional m followed by A
  • (?:[DA]C)? Match optional DA or AC
  • (?!\S) Assert a whitespace boundary to the right

Regex demo

If you meant to match m mA DC AC the optional group should be part of the alternation:

 (?<!\S)-?\d+(?:\.\d+)?(?:/-?\d+(?:\.\d+)?)?(?:m?A|[DA]C)(?!\S)

Regex demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading