Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow angular brackets <> without any characters between them using Regex?

Tags:

java

regex

How to allow angular brackets <> without any characters/space between them using javax regex?Current pattern is like below:

"^[A-Za-z0-9\\s-_]*[A-Za-z0-9][A-Za-z0-9\\s-_]*$"
like image 679
Ram Avatar asked Jan 21 '26 13:01

Ram


1 Answers

Use the | token to create an alternative (match this or that).

^(?:[A-Za-z0-9\s-_]|<>)*[A-Za-z0-9](?:[A-Za-z0-9\s-_]|<>)*$

I changed both the [A-Za-z0-9\s-_]+ at the start and end to (?:[A-Za-z0-9\s-_]|<>)*, which means "match 0 or more times [A-Za-z0-9\s-_] or <>".

[A-Za-z0-9_] can be simplified to \w, so a shorter version of the above would be:

^(?:[\w\s-]|<>)*[A-Za-z0-9](?:[\w\s-]|<>)*$
like image 69
Sweeper Avatar answered Jan 23 '26 02:01

Sweeper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!