-2

Good morning,

Could tou please help on this regular expression request.

I would like all characters before "-", but only if that chain has a lenght of 7 characters

If 5QHTN33-48314742, result 5QHTN33. But if AAA5QHTN33-48314742, then no result.

Thanks for your help.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • 1
    Multiple ways I'd say, but you could give `^[A-Z\d]{7}(?=-\d+$)` a go, or more generic `^([^-]{7})-.*$`. What have you come up with yourself? – JvdV Nov 19 '20 at 08:32

1 Answers1

-2

as '^' refers to start of line and '$' refers to end of line, I highly prefer to say you have to split the line with spaces(regex of split: (\s+) , for java: (\\s+)) so you can use below regex after splitting and use it on every element, hope to be helpful:

(^)((?<word>[\w\W]{7})\-.+)($)

Explanation

(?<word>[\w\W]{7}) will capture your word into a group with name word ,so you can get it easier.

Khayyam
  • 509
  • 3
  • 17