-3

I need to write a regex that matches the string:

HELLO[ID]_world.NNN

Where:

  • 'HELLO' is constant. It must always start with this word
  • 'ID' is any number
  • 'world' is anything
  • 'NNN' is the sequence number in hexadecimal notation, i.e. '000' to 'FFF'

For example valid strings are:

HELLO[345]_something.123
HELLO[23]_BlaBla.FFF

What I have so far is:

\bHELLO\[[0-9]*\]_[a-zA-Z]*\.[0-9]{3}

If I am correct (and maybe I am not) this works for all examples except hexadecimal part.

Could you help me to write this regex?

Biffen
  • 5,354
  • 5
  • 27
  • 32
Raskolnikov
  • 3,103
  • 6
  • 31
  • 72
  • 3
    Have you tried `[0-9a-fA-F]{3}` instead? – tobias_k Sep 04 '18 at 13:15
  • 3
    You say *`'world' is anything`* but you use `[a-zA-Z]*` that only matches letters. Try [`\bHELLO\[[0-9]*]_\S*\.[a-fA-F0-9]{3}\b`](https://regex101.com/r/H77hjv/2/) If you need to only match *entire* strings, replace [`\b...\b` with `^...$`](https://regex101.com/r/H77hjv/3). – Wiktor Stribiżew Sep 04 '18 at 13:16
  • 2
    Also consider using `+` instead of `*` if you don't want to match `HELLO[]_.123` – Aaron Sep 04 '18 at 13:17
  • [`\bHELLO\[[0-9]+\]_[a-zA-Z0-9]+\.[a-zA-Z0-9]{3}`](https://regex101.com/r/nEkeBY/1) – Sagar V Sep 04 '18 at 13:20

1 Answers1

1

To match hexadecimal numbers, you just have to add a-f and/or A-F to the [0-9]{3} group, depending on whether you want to match upper- or lowercase hex numbers or both.

Also, as noted in comments, you might want to change the * to + to disallow empty strings for those parts.

HELLO\[[0-9]+\]_[a-zA-Z]+\.[0-9a-fA-F]{3}

Online-Demo

tobias_k
  • 74,298
  • 11
  • 102
  • 155