@Pattern 注解可以匹配几个最终的 String 属性,而不是它们指向的字符串吗?

发布于 2025-01-11 05:11:03 字数 578 浏览 0 评论 0原文

例如:

import javax.validation.constraints.Pattern;

public class Pojo {

    public final String oneAtt = "one";
    public final String twoAtt = "two";
    public final String threeAtt = "three";

    @Pattern(regexp = <How to match any of those attributes?> )
    private String number;

由于字符串是在属性中定义的,因此目标是避免在模式中重复字符串。所以,我想避免这种情况:

@Pattern(regexp = "^(one|two|three)" )

并寻找类似的内容:

@Pattern(regexp = oneAtt|twoAtt|threeAtt )

这当然在 Java 语言中是无效的。

问题是是否有办法避免重复模式中的字符串。

For example:

import javax.validation.constraints.Pattern;

public class Pojo {

    public final String oneAtt = "one";
    public final String twoAtt = "two";
    public final String threeAtt = "three";

    @Pattern(regexp = <How to match any of those attributes?> )
    private String number;

Since the strings are defined in the attributes, the goal is to avoid repeating the strings in the pattern. So, I want to avoid this:

@Pattern(regexp = "^(one|two|three)" )

And looking for something like:

@Pattern(regexp = oneAtt|twoAtt|threeAtt )

Which of course it is invalid in the Java language.

The question is if there is a way to avoid duplicating the strings in the pattern.

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

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

发布评论

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

评论(1

中二柚 2025-01-18 05:11:03

将这三个字符串设置为static,然后您可以直接在注释中使用它们:

public static final String oneAtt = "one";
public static final String twoAtt = "two";
public static final String threeAtt = "three";

@Pattern("^(" + oneAtt + "|" + twoAtt + "|" + threeAtt + ")")
private String number;

Make the three strings static then you can use them directly in the annotation:

public static final String oneAtt = "one";
public static final String twoAtt = "two";
public static final String threeAtt = "three";

@Pattern("^(" + oneAtt + "|" + twoAtt + "|" + threeAtt + ")")
private String number;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文