1

I am trying to find a pattern for strings which have any other characters except

+,[0-9],- and \

. Eg: +123-\ should not be matched +232aa should be matched

I tried this: ^[a-zA-Z?\§$%&=?{}\\]*$

  • a string like asdad$ is matched
  • a string like +331234-44 is not matched which is what I want
  • a string like 123asd or asdad123$ is also not matched but should me matched

I am not sure what I did wrong here

Phani Shashank
  • 98
  • 1
  • 10

1 Answers1

1

I think this is what you want:

^.*[^+0-9\\-]+$|^[^+0-9\\-]+.*$

Here is a working demo:

https://regex101.com/r/xsvqkS/2

I think you have the , in your blockquote to separate the 'items' and it's not part of your item list. I'm using a [^ ... ] as a negative character class. So anything NOT in that list will match. Note that the \ needs to be escaped and I put the - hyphen at the end so it can't be confused as a range of characters.

I'm sure there is a more concise regex that can be written, but I usually lean on the side of being very readable. That why it's easy to update down the road.

sniperd
  • 5,124
  • 6
  • 28
  • 44
  • From OP: "`+232aa` should be matched" but your regex doesn't. – Toto Oct 24 '18 at 15:07
  • ah.. I misunderstood. I was thinking "match" as in he's looping a list and a match happened, not the whole item now turns into a match. I'll update. – sniperd Oct 24 '18 at 15:08
  • try this instead `^.*[^+0-9\\\n-]+$|^[^+0-9\\\n-]+.*$|^.*[^+0-9\\\n-]+.*$` because I assume if OP says this is ok `+232aa` then also this is ok `+232aa232` to account for any presence of allowed character no matter the location in a single line. – Hadi Farah Oct 24 '18 at 15:20
  • @HadiFarah ah, you might be right. Now that I re-read his question I'm not actually sure what he wants :) Like is the data a list, or a huge text file? – sniperd Oct 24 '18 at 15:24
  • 1
    I just assume 1 line text with break lines if none is specified. [My Regex](https://regex101.com/r/S7wz7h/1) – Hadi Farah Oct 24 '18 at 15:24
  • The issue is I have a table with phone numbers with some wired characters, after analysis the above mentioned pattern is what I have seen. That is the reason for the question – Phani Shashank Oct 24 '18 at 16:56