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

(\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

Leave a Reply