0

I have a string of text and numbers which to all intents and purposes looks like a long string of random text.

I need to detect multiple + or multiple - which are either next to each other or spread out throughout the string.

So, for example I need to detect these:

abc+abc+abc-abc-

or

abc++abc--

abc could be numbers or characters. The text could contain zero or one + and zero or one - in any order, at the beginning or anywhere through.

Could someone please assist with a regex (vba compatible) which would assist in determining these?

Many thanks

cosmarchy
  • 686
  • 3
  • 9
  • 21
  • 1
    According to your description the whole text would match. Can you be more precise as to where a match should start and end? – trincot Nov 07 '17 at 20:49
  • "zero or one + and zero or one -" that doesn't seem to match your second example or the previous description "multiple + or multiple - which are either next to each other or spread out"? Also it's unclear what you mean by "detect", and why Instr() is insufficient here. – Tim Williams Nov 07 '17 at 20:51
  • Looking for this? [`[a-z\d+-]+`](https://regex101.com/r/WzTG2n/1) – trincot Nov 07 '17 at 21:22
  • So, can you provide any negative test cases? – Nissa Nov 07 '17 at 22:02
  • 1
    I think I need to revisit this - whilst putting together information to respond to this post, I believe there may be more to this so I really think I'd need to think about this further to try and rationalize the options and potentially add more as this question was only a small part of the overall problem :( – cosmarchy Nov 08 '17 at 18:42
  • To get only the + or - symbols you can use this regex: `[+|-]` and the [demo](https://regex101.com/r/cQBx93/2), however, RegEx isn't necessary. You must be more clear on your question. And refer to [this](https://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops) – danieltakeshi Nov 09 '17 at 13:55

1 Answers1

0

You don't need to use a single regular expression. Using other methods can be a lot clearer and more straightforward: remove matches for [^+-] to filter out the characters you don't want, then use ([+-])\1 to do the final validation.

Nissa
  • 4,636
  • 8
  • 29
  • 37