私はかなりgrailsで新しいです。私がしたいのは、フィールドは英数字と特殊文字、英字と数字のみの組み合わせを受け入れることができ、ユーザーが特殊文字のみを入力すると、システムはユーザーにエラーを表示する必要があります。 私は制約を使ってデータを検証しましたが、苦労しましたが、フィールドが特殊文字だけの入力を受け付けない正規表現をどのように設定することができましたか? あなたの知識を共有してくれてありがとう。正規表現バリデーション - grails
0
A
答えて
2
私は私はあなたの問題を理解すると思います私が間違っていれば私を修正してください。少なくとも1文字または数字がある限り、入力は有効です。つまり、文字や数字(特殊文字のみ)がない場合、入力は無効ですか?
これが動作するかどうかを参照してください:ここで
/^.*[A-Za-z0-9].*$/
は私の小さなグルーヴィーなテストです:
import java.util.regex.Matcher
import java.util.regex.Pattern
def pattern = ~/^.*[A-Za-z0-9].*$/
assert pattern.matcher("abc").matches()
assert pattern.matcher("ABC").matches()
assert pattern.matcher("abc123").matches()
assert pattern.matcher("123").matches()
assert pattern.matcher("abc!").matches()
assert pattern.matcher("!abc").matches()
assert pattern.matcher("1!bc").matches()
assert pattern.matcher("!.~").matches() == false
を説明:
/ regex start
^ start of string
.* any character (0 or more times)
[A-Za-z0-9] at least 1 letter or number
.* any character (0 or more times)
$ end of string
/ regex end
+0
これは動作します!共有していただきありがとうございます。 – antibry
1
Grailsのlookaroundをサポートしている場合、私は知らないが、それがない場合は、この正規表現は、あなたのために動作します:
/(?=^[\pL\pN!:;]+$)(?!^[!:;]+$)/
説明:
/ : regex delimiter
(?= : begin positive lookahead
^ : start of string
[\pL\pN!:;]+ : a letter, a digit or a special char one or more times
$ : end of string
) : end of lookahead
(?! : begin negative lookahead
^ : start of string
[!:;]+ : a special char one or more times
$ : end of string
) : end of lookahead
/ : regex delimiter
関連する問題
- 1. Struts2の+バリデーション+正規表現
- 2. 正規表現バリデーター:クライアント側のバリデーション
- 3. 正規表現の正規表現
- 4. 正規表現 - 正規表現
- 5. 正規表現:表現
- 6. 正規表現
- 7. 正規表現
- 8. 正規表現
- 9. 正規表現
- 10. 正規表現
- 11. 正規表現
- 12. 正規表現
- 13. 正規表現
- 14. 正規表現
- 15. 正規表現
- 16. 正規表現
- 17. 正規表現
- 18. 正規表現
- 19. 正規表現
- 20. 正規表現
- 21. 正規表現
- 22. 正規表現
- 23. 正規表現
- 24. 正規表現
- 25. 正規表現
- 26. 正規表現
- 27. 正規表現
- 28. - 正規表現
- 29. 正規表現
- 30. 正規表現
を一つだけ正規表現でこれを行うことはできません私の知る限り。 – m0skit0