-3

Hi I have created a regular expression that contains the letter mentioned in the title of the question. Below is the expression that I have created for a text:

^[0-9A-Z-():,&/\'\\.\s]+$

However, this is not working in case when I am entering "&" or a comma. I mean it is not allowing & and comma in the text. Am I missing something or this is not a valid expression.

anubhava
  • 761,203
  • 64
  • 569
  • 643
Running Rabbit
  • 2,634
  • 15
  • 48
  • 69
  • 1
    Just use [Regex101](https://regex101.com/) if you don’t know. It’s not matching most characters because they aren’t escaped. – Sebastian Simon Oct 05 '16 at 06:20
  • Possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) — because this ultimately answers the real question: you’ve typed a regex, don’t know why it doesn’t work, thus don’t know what it even means. The answers contain automated tools to explain regexes. – Sebastian Simon Oct 05 '16 at 06:22
  • Where and how is the pattern used? Please share the code. This is no duplicate question, but certainly offtopic since no code to repro the issue is provided - [the regex matches `&`](http://regexstorm.net/tester?p=%5e%5b0-9A-Z-()%3a%2c%26%2f%5c%27%5c%5c.%5cs%5d%2b%24&i=AAA%26). Is it used in some XML? – Wiktor Stribiżew Oct 05 '16 at 06:43
  • @WiktorStribiżew: It is not using XML. I am just using C# language for regular expression to match – Running Rabbit Oct 05 '16 at 06:44
  • @Yash - *Please share the code*. I vote to close the question unless you provide repro data. – Wiktor Stribiżew Oct 05 '16 at 06:45
  • @WiktorStribiżew: When I am entering "&", it is adding "&;;" automatically. This is the reason why it is not accepting & – Running Rabbit Oct 05 '16 at 07:23
  • @Yash, now, you should provide the details explaining HOW COME `&` is changed to a character entity. You say there is no XML involved, I strongly doubt that. – Wiktor Stribiżew Oct 05 '16 at 07:30
  • Do you agree your regex is working well, but your other code serializes the input text? The `&` does not match the regex due to `;`. – Wiktor Stribiżew Oct 05 '16 at 09:23
  • @WiktorStribiżew: whenever, I was entering "&" from the UI, it is treating it as "&" and because of ";" it is not matching it. I had to explicitly handle it thru code. – Running Rabbit Oct 06 '16 at 05:26
  • That is crystal clear. However, since you haven't described your isssue in the question it is off topic. No one but you can answer the question, then why ask? Either share the code to repro or it shouldbe closed. – Wiktor Stribiżew Oct 06 '16 at 05:44

2 Answers2

1

The -, when inside square brackets takes a special meaning, and denotes a range.

[A-Z] denotes a range of upper case letters between A and Z. In your expression, the issue is this: Z-(, since the engine does not know what to do with such a range.

Change this: ^[0-9A-Z-():,&/\'\\.\s]+$ to this: ^[0-9A-Z():,&/\'\\.\s-]+$. Notice that now, the - is at the end.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

Use this Regular Expression:

^[A-Za-z0-9&(),#&:/\s-]+$
Nachan C
  • 79
  • 8