Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex for any symbol?

Is there a regex which accepts any symbol?

EDIT: To clarify what I'm looking for.. I want to build a regex which will accept ANY number of whitespaces and the it must contain atleast 1 symbol (e.g , . " ' $ £ etc.) or (not exclusive or) at least 1 character.

like image 609
Skizit Avatar asked Dec 03 '10 12:12

Skizit


People also ask

What is the regex for any character?

Matching a Single Character Using Regex By default, the '. ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do I get special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What is the regex for special characters in Java?

Special Characters in Java Regular Expressions Regex is a type of textual syntax representing patterns for text matching. Regular expressions make use of special characters such as . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ .


1 Answers

Yes. The dot (.) will match any symbol, at least if you use it in conjunction with Pattern.DOTALL flag (otherwise it won't match new-line characters). From the docs:

In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.


Regarding your edit:

I want to build a regex which will accept ANY number of whitespaces and the it must contain atleast 1 symbol (e.g , . " ' $ £ etc.) or (not exclusive or) at least 1 character.

Here is a suggestion:

\s*\S+
  • \s* any number of whitespace characters
  • \S+ one or more ("at least one") non-whitespace character.
like image 156
aioobe Avatar answered Oct 14 '22 05:10

aioobe