Home > Software design >  How do I extract an item number that looks roughly like 95E10 from a string using ruby regex
How do I extract an item number that looks roughly like 95E10 from a string using ruby regex

Time:10-15

Given strings that look like one of the two following

"Stop Blasting to Move Track, Continue Blasting 95E10 FF"

"Continue Blasting I.D. on Exchanger 95HE03A"

how can I used ruby regex to extract item numbers 95E10 and 95HE03A respectively?

CodePudding user response:

Try the following regex:

\d [A-Z] \d [A-Z]*

Explanation

The \d 's match the numbers on either side of the string. [A-Z] in the middle matches the letters in the middle, whilst the [A-Z]* matches zero or more letters at the end.

CodePudding user response:

Use regex:

\d (([A-Z] )?(\d )?)*

Demo: enter image description here

  • Related