Need to restrict text with [ at beginning

Viewed 53
@Pattern("^\\[")

Does not work.

Sample inputs:

Welcome[Hello - Pass

Welcome[ - Pass

[Welcome - Fail

- Pass

@Pattern("^(?!\\[).*")

was suggested from stackoverflow. But when inserting new line char (\n) it doesn't seems working

public final static String FREE_TEXT_FIELD_VALIDATION_PATTERN = "^(?!\\[).*";

@Pattern(regexp = FREE_TEXT_FIELD_VALIDATION_PATTERN, message = FREE_TEXT_FIELD_VALIDATION_ERROR) 
public String ruleDescription;
1 Answers
^\[.*$

^ = start of string
\[ = escaped '[' character
.* = zero or more of any character except newline

Welcome[Hello - Fail

Welcome[ - Fail

[Welcome - Pass

You can test here https://regexr.com/

Edit

If you want to match anything who NOT START with [ but allow line break

^(?!(\[))\w+((.|\n)*)$

Welcome[Hello - Pass

Welcome[ - Pass

[Welcome - Pass

[Welcome

test.    - PASS

Edit 2

^(?!(\[))\w+((.|\n)*)$|^$

We add |^$ for allow empty string

| is used to denote alternates this|that.

Related