-2

I need a help regarding regular expression.

I have to match string like this: "dãasc abd"

Pattern that i have used:

([^\u0000-\u007F] |\\w|^[a-zA-Z0-9-\\s]*$)+

but this pattern does not allow whitle spaces in two strings

Please help me for find out correct pattern for this kind of string.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 3
    It isn't clear what you want to match... That reges seems to be a mashup of things... – xanatos Mar 09 '17 at 10:18
  • Maybe `@"^\w+(?:[-\s]\w+)*$"`? It will match `Как`, `Как дела`, `Как-никак`, `Ростов-на-Дону`. See [demo](http://regexstorm.net/tester?p=%5e%5cw%2b%28%3f%3a%5b-%5cs%5d%5cw%2b%29*%5cr%3f%24&i=%d0%9a%d0%b0%d0%ba%0d%0a%d0%9a%d0%b0%d0%ba+%d0%b4%d0%b5%d0%bb%d0%b0%0d%0a%d0%9a%d0%b0%d0%ba-%d0%bd%d0%b8%d0%ba%d0%b0%d0%ba%0d%0a%d0%a0%d0%be%d1%81%d1%82%d0%be%d0%b2-%d0%bd%d0%b0-%d0%94%d0%be%d0%bd%d1%83&o=m). – Wiktor Stribiżew Mar 09 '17 at 10:20
  • I want to match swedish characters so I put [^\u0000-\u007F] this pattern.I want to match "dãa3sc abd" this type of string. – NullException Mar 09 '17 at 10:30
  • Yes, `\w` will match any alphabet letters. See, it also matches Russian words. What are the exact requirements? – Wiktor Stribiżew Mar 09 '17 at 10:34
  • That isn't what that regex is matching... It is matching non-ascii characters followed by a space (`[^\u0000-\u007F] `) OR any word character (that is any letter with or without diacritics or any number, see http://stackoverflow.com/a/2998550/613130) or a whole string composed only from letters, numbers and spaces. – xanatos Mar 09 '17 at 10:35
  • Try `^(\\w||\\s)+$` – xanatos Mar 09 '17 at 10:35
  • Besides, the requirements, please also post the code you are using. – Wiktor Stribiżew Mar 09 '17 at 10:37
  • Thanks for your help.It's matching but it is not supported by java script as I'm getting client side validation message which vanishes within second so to avoid that I have used [^\u0000-\u007F]. – NullException Mar 09 '17 at 10:41
  • I'm using it in MVC as attribute to property.Property [RegularExpression(RegexKeys.AllLanguageCharacters , ErrorMessage = "Please enter a valid string,no special characters allowed")] – NullException Mar 09 '17 at 10:42
  • I also don't want to allow underscore – NullException Mar 09 '17 at 10:46
  • @Shubh's Ok, try with `^([A-Za-z0-9]|\\s|[^\u0000-\u007F)+$` – xanatos Mar 09 '17 at 11:08
  • Does it mean the regex pattern should also be compatible with JavaScript regex engine? Is it also run on the client side? – Wiktor Stribiżew Mar 09 '17 at 11:22
  • @WiktorStribiżew Yes.It is working fine now.Thanks – NullException Mar 09 '17 at 11:46
  • @WiktorStribiżew..How can I accept your answer?I'm not finding any option to up vote.. – NullException Mar 09 '17 at 11:47
  • Is the `@"^\w+(?:[-\s]\w+)*$"` pattern working for you? But it accepts underscores. In order to not match them you need `@"^[\w-[_]]+(?:[-\s][\w-[_]]+)*$"` - but that will only work on the server side, in .NET. – Wiktor Stribiżew Mar 09 '17 at 11:50
  • No @"^\w+(?:[-\s]\w+)*$" pattern not working for me as it accepts underscore. but ^([A-Za-z0-9]|\\s|[^\u0000-\u007F)+$ pattern worked on both client and server side – NullException Mar 09 '17 at 12:01
  • I think what worked was `^([A-Za-z0-9]|\\s|[^\u0000-\u007F])+$` – Wiktor Stribiżew Mar 09 '17 at 12:02
  • @xanatos: If you wish, you may post your above regex. – Wiktor Stribiżew Mar 09 '17 at 12:14

2 Answers2

1

You need to remove one slash in your regex, where you define white space:

Try this:

@"([^\u0000-\u007F]|\s|[a-zA-Z0-9-])+"
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You could try with:

^([A-Za-z0-9]|\\s|[^\u0000-\u007F])+$

It means that the string must be entirely composed of ASCII letters or numbers [A-Za-z0-9] or spaces \\s or non-ASCII characters [^\u0000-\u007F] (it means characters outside the 00-7F range, so >7F

xanatos
  • 109,618
  • 12
  • 197
  • 280