質問

I need to create a regex validation rule like below:

  1. Contain alpha numeric only

  2. Can have -, _ and /

  3. Can have one space in the beginning and many at the end

  4. Can mix symbols

For example:

aaa-bbb/ccc
aaa
1223/aaa-bbb 
aaa-bbb-ccc

I try with the following code and its not working for mix symbol and for -. Any idea how to handle it?

@"^((?:/[a-za-z0-9]+)+/?|/?(?:[a-za-z0-9]+/)+)[a-za-z0-9]*$"
役に立ちましたか?

解決

Does this regex work for you:

/^ ?[a-zA-Z0-9_\/-]+ *$/gm

Demo. That is made to do exactly what you asked for.

他のヒント

Here is an analysis of your regex. As you can see it is far too complicated and nowhere near what you're looking for.

Assuming I'm interpreting your conditions correctly, your basic regex should probably look something like:

@"^[a-zA-Z0-9/_-]+$"

That's it. It contains alpha-numeric only, also allowing -, _ and /, allowing mixed symbols. I'm afraid I don't know what you mean in condition 3, but maybe adding \s? just after the ^ and \s* just before the $ should do it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top