0

I have a list of products that consist of a product ID concatenated with a size ID. For example, 8207RB may be the product-ID and A is the size-ID, so I would have 8207RBA. Right now, I only want to find products with size-ID A at the end. Unfortunately, I also have some products with size-ID AA, or AAA. I do not want to match those. How could I create a regular expression that only matches a product with a single A at the end, rather than repeated A's?

  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Jun 08 '20 at 15:54
  • You have not given us the pattern of the product id, which is relevant information. I will assume it consists of 4 digits followed by two capital letters, as in your example. One option is to append a *word boundary* to the end of your regex: `\d{4}[A-Z]{2}A\b`. This asserts that `'A'` is not followed by a word character; that is, is a letter, digit or underscore. It therefore would prevent a match of `'1000BAA'`, `'1000BAa'`, `'1000BA9'` and `'1000BA_'`. Suppose one only wants to prevent a match of the first two of these four strings... – Cary Swoveland Jun 08 '20 at 17:53
  • ...We could do that by using a *positive lookahead* to assert that `'A'` is at the end of the line or string or is followed by a character other than a letter: `\d{4}[A-Z]{2}A(?=$|[^A-Za-z])`. A better way is to use a *negative lookahead* to assert that `'A'` is *not* followed by a letter: `\d{4}[A-Z]{2}A(?![A-Za-z])`... – Cary Swoveland Jun 08 '20 at 17:53
  • ...If your product id does not necessarily start at the beginning of a line or string you will want to do something similar at the beginning of your regex: inserting a word boundary or a *positive lookbehind* (`(?<=...)`) or *negative lookbehind* (`(? – Cary Swoveland Jun 08 '20 at 17:54

0 Answers0