1

I have this text:

x A1 ,A2-A4 xx, xxx A5  xxxx  

I have this regular expression:

[A-Z]{1,3}-?\d{1,3}  

It gets 4 matches:

A1,A2,A4,A5  

But I need to modify this regular expression to get these 3 matches:

A1,A2-A4,A5    

Can anyone help me?

1 Answer 1

1

Just double the expression:

[A-Z]{1,3}-?\d{1,3}(?:-[A-Z]{1,3}-?\d{1,3})?

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

This works exactly as I need, thanks. I hope I will never need regular expressions again...
@Ondřej " hope I will never need regular expressions again" A ha, like it's going to happen ;-)
@Ondřej better learn them (it's not that hard), and see how much code you won't need to write ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.