正则表达式验证 - grails

发布于 2025-01-08 08:13:12 字数 166 浏览 7 评论 0原文

我对 grails 还很陌生。我现在在使用匹配进行验证时遇到了一些问题。我想要发生的是,一个字段可以接受字母数字和特殊字符的组合,仅字母和数字,并且如果用户仅输入特殊字符,系统应提示用户错误。 我使用匹配约束来验证数据,但我很难设置正则表达式,使该字段不接受仅包含特殊字符的输入。 请帮助我..非常感谢您分享您的知识。

im pretty new in grails.. im having a little problem right now on validation using matches. What I wanted to happen is that a field can accept combination of alphanumeric and special characters, letters only and numbers only, and if the user inputs special characters only, the system should prompt the user an error.
i used matches constraints to validate the data, and im having a hard time how could i set the regex where the field will not accept an input with special characters only.
please help me.. thanks a lot for sharing your knowledge.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2025-01-15 08:13:12

我想我理解你的问题,但如果我错了,请纠正我。只要输入至少 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

I think I understand your problem, but correct me if I'm wrong. The input is valid as long as there is at least 1 letter or number, right? In other words, if there isn't a letter or number (only special characters), then the input is invalid?

See if this works:

/^.*[A-Za-z0-9].*$/

Here is my little groovy test:

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

Explained:

/             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
拥有 2025-01-15 08:13:12

我不知道 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

I don't know if grails supports lookaround, but if it does, this regex will work for you:

/(?=^[\pL\pN!:;]+$)(?!^[!:;]+$)/

explanation:

/                 : 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文