Regex to Match: !$%^&*()_+|~-=`{}[]:";'<>?,./ - java

I know this a duplicate of a question with almost the identical name, however, I can't get it to work in Android what so ever!
I am trying this: Regex to Match Symbols:
public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\\";'<>?,.\/]");
However, this isn't working. Does anyone know the correct method of applying this pattern?
P.S. Complete noob at Regex. :D
From here originally - Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./
Error Message: Syntax error on token(s), misplaced construct(s)
UPDATE: Added extra backslashes...fixed a lot of em, now gets error from ; onwards. Using Eclipse.

I think your problem is the "
public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]");
^
it is ending your string, so you should escape it. Also you need to remove the backslash before the slash, it is no special character.
public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,./]");
OK, once more, you wanted to match the backslash, not to escape the slash, then we end up here:
public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");
now it is the same answer, than jdb's, so +1 to him for being quicker.

How about that?
Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");

In a character class, only [ and ] have special meaning, so you need to escape them. Plus in Java, you need to escape with an extra backslash. That's the problem specifically with Java. So, you need to use \\[ and \\]. And yes, you need to escape " with single backslash, in a string literal.
Apart from that, a hyphen when used somewhere in the middle, has also a special meaning. If you want to match a hyphen, you need to use it at the ends.
Rest of the characters, don't need to be escaped. They are just ordinary characters.
So, your pattern should be like this: -
Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,./]");
And if you want to match backslash (\) also, then use this: -
Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");

Related

Regex to allow only Numbers, alphabets, spaces and hyphens - Java

Need to allow user to enter only Numbers or alphabets or spaces or hyphens OR combination of any of the above.
and i tried the following
String regex = "/^[0-9A-Za-z\s\-]+$/";
sampleString.matches(regex);
but it is not working properly. would somebody help me to fix please.
Issue : your regex is trying to match / symbol at the beginning and at the end
In java there is no need of / before and after regex so use, java!=javascript
"^[0-9A-Za-z\\s-]+$"
^[0-9A-Za-z\\s-]+$ : ^ beginning of match
[0-9A-Za-z\\s-]+ : one or more alphabets, numbers , spaces and -
$ : end of match
You are close but need to make two changes.
The first is to double-escape (i.e., use \\ instead of \). This is due to the weirdness of Java (see the section "Backslashes, escapes, and quoting" in Javadoc for the Pattern class). The second thing is to drop the explicit reference to the start and end of the string. That's going to be implied when using matches(). So the correct Java code is
String regex = "[0-9A-Za-z\\s\\-]+";
sampleString.matches(regex);
While that will work, you can also replace the "0-9" reference with \d and drop the escaping of the "-". That gives you
String regex = "[\\dA-Za-z\\s-]+";

Add Dash to Java Regex

I am trying to modify an existing Regex expression being pulled in from a properties file from a Java program that someone else built.
The current Regex expression used to match an email address is -
RR.emailRegex=^[a-zA-Z0-9_\\.]+#[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
That matches email addresses such as abc.xyz#example.com, but now some email addresses have dashes in them such as abc-def.xyz#example.com and those are failing the Regex pattern match.
What would my new Regex expression be to add the dash to that regular expression match or is there a better way to represent that?
Basing on the regex you are using, you can add the dash into your character class:
RR.emailRegex=^[a-zA-Z0-9_\\.]+#[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
add
RR.emailRegex=^[a-zA-Z0-9_\\.-]+#[a-zA-Z0-9_-]+\\.[a-zA-Z0-9_-]+$
Btw, you can shorten your regex like this:
RR.emailRegex=^[\\w.-]+#[\\w-]+\\.[\\w-]+$
Anyway, I would use Apache EmailValidator instead like this:
if (EmailValidator.getInstance().isValid(email)) ....
Meaning of - inside a character class is different than used elsewhere. Inside character class - denotes range. e.g. 0-9. If you want to include -, write it in beginning or ending of character class like [-0-9] or [0-9-].
You also don't need to escape . inside character class because it is treated as . literally inside character class.
Your regex can be simplified further. \w denotes [A-Za-z0-9_]. So you can use
^[-\w.]+#[\w]+\.[\w]+$
In Java, this can be written as
^[-\\w.]+#[\\w]+\\.[\\w]+$
^[a-zA-Z0-9_\\.\\-]+#[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
Should solve your problem. In regex you need to escape anything that has meaning in the Regex engine (eg. -, ?, *, etc.).
The correct Regex fix is below.
OLD Regex Expression
^[a-zA-Z0-9_\\.]+#[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
NEW Regex Expression
^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
Actually I read this post it covers all special cases, so the best one that's work correctly with java is
String pattern ="(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";

Regex to match \a574322 in Java

I have long string looking like this: \c53\e59\c9\e28\c20140326\a4095\c8\c15\a546\c11 and I need to find expressions starting with \a and followed by digits. For example: \a574322
And I have no idea how to build it. I can't use:
Pattern p = Pattern.compile("\\a\\d*");
because \a is special character in regex.
When I try to group it like this:
Pattern p = Pattern.compile("(\\)(a)(\\d)*");
I get unclosed group error even though there is even number of brackets.
Can you help me with this?
Thank you all very much for solution.
You can use this regex:
\\\\a\\d+
Code Demo
Since in Java you need to double escape the \\ once for String and second time for regex engine.
You have to change your regex to:
Pattern p = Pattern.compile("(\\\\a\\d+)");
The regex is:
(\\a\d+)
The idea is to escape a backslash and then also escape the backslash for \a, and match digits too.
You need 4 \.
2 to indicate to regex that it is not a special character, but a plain \, and 2 for each to tell the Java String that these are not special characters either. So you need to represent it in code this way:
"\\\\a\\d*"
Which is actually the regex \\a\d*
\\(a)[0-9]+ this should work
you can't try your regexps on this page or some similar
http://regex101.com/

How to search for a special character in java string?

I am having some problem with searching for a special character "(".
I got a java.util.regex.PatternSyntaxException exception has occurred.
It might have something to do with "(" being treated as special character.
I am not very good with pattern expression. Can someone help me properly search for the escape character?
// I need to split the string at the "("
String myString = "Room Temperature (C)";
String splitList[] = myString.split ("("); // i got an exception
// I tried this but got compile error
String splitList[] = myString.split ("\(");
Try one of these:
string.split("\\(");
string.split(Pattern.quote("("));
Since a string split takes a regular expression as an argument, you need to escape characters properly. See Jon Skeet's answer on this here:
The reason you got an exception the first time is because split() takes a regular expression as argument, and ( has a special meaning there, as you suggest. To avoid this, you need to escape it using a \, like you tried.
What you missed, is that you also need to escape your backslashes with an extra \ in Java, meaning you need a total of two:
String splitList[] = myString.split ("\\(");
You need to escape the character via backslashes: string.split("\\(");
( is one of regex special characters. To escape it you can use e.g.
split(Pattern.quote("(")),
split("\\Q(\\E"),
split("\\("),
split("[(]").

java eclipse regex cant "\+"

I need to check a String is "\++?" which will match something like +6014456
But I get this error message invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\) .... why?
It's giving you an error because "\++?" isn't a valid Java literal - you need to escape the backslash. Try this:
Pattern pattern = Pattern.compile("\\++?");
However, I don't think that's actually the regular expression you want. Don't you actually mean something like:
Pattern pattern = Pattern.compile("\\+\\d+");
That corresponds to a regular expression of \+\d+, i.e. a plus followed by at least one digit.
I think you should use two backslashes. One for escaping the second (because it's a java string), the second for escaping the + (because it's a special character for regex).
shouldn't it be more like "\\+?" ?
Pattern pattern = Pattern.compile("\\++?");
System.out.println(pattern.matcher("+9970").find());
works for me

Categories