You simply need to escape the special characters. Try:
[a-zA-Z0-9\-#\.\(\)\/%&\s]{0,19}
You can test your regular expressions on http://rubular.com/
Answer from Andy Triggs on Stack OverflowYou simply need to escape the special characters. Try:
[a-zA-Z0-9\-#\.\(\)\/%&\s]{0,19}
You can test your regular expressions on http://rubular.com/
Your regex is incorrect in at least one way - if you're considering a hyphen to be a "special character", then you should put it at the beginning or end of the range. So: [a-zA-Z0-9#.()/%&\s-]{0,19}.
Characters that are "special" within the context of the regex itself are often not parsed if they're inside a range. So you're fine with ., ( and ). But check your parser to make sure that it understands what \s means. It might be simpler just to put a space.
Also, if your regex parser tends to delimit the regex with slashes, then you may have to escape the slash in the middle of the range: [a-zA-Z0-9#.()\/%&\s-]{0,19}.
Try this regex
[\w-]+
Which matches all below
ABC12-xy
ABCD
ABC12_12
12-AB_xy
use [\w-]+ to match the entire string. You can use ^ and $ to specify the start and the end of the line. For example ^[\w-]+$ would match the entire line only if the line has all word or - characters.
It looks like it will not match a whole string containing a mix of alphanumerics and symbols because of the OR in the middle.
e.g. it wont match abcABC()+, but will match abcABC and will match ()+
Try:
([a-zA-Z0-9\\'\\(\\+\\)\\,\\-\\.\\=]+)
Hope this helps!
Use following regular expression:
^[-+=(),'.a-zA-Z0-9]+$
If you want allow zero-length string, replace + with *:
^[-+=(),'.a-zA-Z0-9]*$
I'm new to regular expressions in Java and I need to validate if a string has alphanumeric chars, commas, apostrophes and full stops (periods) only.
I suggest you use the \p{Alnum} class to match alpha-numeric characters:
Pattern p = Pattern.compile("[\\p{Alnum},.']*");
(I noticed that you included \s in your current pattern. If you want to allow white-space too, just add \s in the character class.)
From documentation of Pattern:
[...]
\p{Alnum}An alphanumeric character:[\p{Alpha}\p{Digit}][...]
You don't need to include ^ and {1, ...}. Just use methods like Matcher.matches or String.matches to match the full pattern.
Also, note that you don't need to escape . within a character class ([...]).
Pattern p = Pattern.compile("^[a-zA-Z0-9_\\s\\.,]{1," + s.length() + "}$");
Considering you want to check for ASCII Alphanumeric characters, Try this:
"^[a-zA-Z0-9]*$". Use this RegEx in String.matches(Regex), it will return true if the string is alphanumeric, else it will return false.
public boolean isAlphaNumeric(String s){
String pattern= "^[a-zA-Z0-9]*$";
return s.matches(pattern);
}
If it will help, read this for more details about regex: http://www.vogella.com/articles/JavaRegularExpressions/article.html
In order to be unicode compatible:
^[\pL\pN]+$
where
\pL stands for any letter
\pN stands for any number
Use the ^ and $ anchors to instruct the regex engine to start matching from the beginning of the string and stop matching at the end of the string, so taking your regex:
^(\\W).{1,1}(\\w+)$
Please take a look at this Oracle (Java) tutorial on regular expressions.
Try this regexp: \w*\W?\w* (Java string: "\\w*\\W?\\w*")
This expression has a drawback of matching zero-length strings. If your input must have exactly one special character, remove the question mark ? from the expression.
You can use the below regex to achieve your purpose:
^[\w][\S]{0,8}$
Explanation of the above regex:
^- Represents the start of the line.
[\w]- Matches a character from [0-9a-zA-Z_]. If you do not want_(underscore) then provide the character class manually.[0-9A-Za-z]
[\S]{0,8}- Matches any non-space character 0 to 8 times.
$- Represents end of the line.

You can find the demo of the above regex here.
Implementation in java:(You can modify the code accordingly to suit your requirements)
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main
{
private static final Pattern pattern = Pattern.compile("^[\\w][\\S]{0,8}$", Pattern.MULTILINE);
public static void main(String[] args) {
final String string = "Hello!@+\n"
+ "t123123\n"
+ "H123!#\n"
+ "@adadsa\n"
+ "@3asdasd\n"
+ "%sdf\n"
+ "Helloworld1";
Matcher matcher = pattern.matcher(string);
while(matcher.find())
System.out.println(matcher.group(0));
}
}
You can find the sample run of the above implementation in here.
I think this should work given your description:
^[a-zA-Z0-9]\S{0,8}$
It's not clear to me that you want to put ^ at the front and $ at the end. That will mean that the entire line must match. If you just want the entire string to match, adding these won't change anything and your pattern won't be useful for searching.
If you haven't looked, the Pattern class javadocs have a lot of helpful info including supported character classes. Those are the Java 7 docs but I doubt these have changed much.
Since you want to split only on £ or $ I would suggest to put them in your own character class [£$] instead using predefined one which contains many characters that probably shouldn't be split. So try something like split("\\s|(?<=[£$])") which will split on
\\s- every whitespace(?<=[£$])- every place that has£or$before it like$|100(|represents such place). Mechanism used here is called look-behind.
Demo
for (String s : "I have $100 and £200".split("\\s|(?<=[£$])"))
System.out.println(">" + s);
output:
>I
>have
>$
>100
>and
>£
>200
You could get the output you are asking with the following java code,
Pattern pattern = Pattern.compile("(\\$)|(\\w+)");/*(\\w*)"); changed to \\w+ to avoid empty matches, based on AlanMoore's remark*/
Matcher matcher = pattern.matcher("I have $100");
while(matcher.find()){
// if(matcher.group().isEmpty())continue;
System.out.println(matcher.group());
}
You could match either == or a question mark in a capturing group, and use a backreference to group 1 using \1
You can use the character class [a-zA-Z0-9] or extend it to use \w (Note to use A-Z instead of A-z)
(==|\?)\h*\w+(?:\h+\w+)*\h*\1
(==|\?)Capture group 1, match either==or?\h*Match 1+ horizontal whitespace chars\w+Match 1+ word chars(?:\h+\w+)*Optionally repeat matching the horizontal whitespace chars and word chars\h*Match 1+ horizontal whitespace chars\1Backreference to group 1
Regex demo
In Java
String regex = "(==|\\?)\\h*\\w+(?:\\h+\\w+)*\\h*\\1";
This regex (== [A-Za-z0-9 _,.?!"'\-]+ ==)|(\?[A-Za-z0-9 _,.?!"'\-]+\?) matches alphanumeric characters and punctuation between 2 equal signs (and a space too) or 2 question marks (without spaces). You may add other characters between the square brackets if you wish (for example, ">").
[A-Za-z0-9 _,.?!"'\-] matches letters, numbers, underscores, commas, periods, question marks, exclamation marks, double quotes, single quotes, and hyphens.
Link to online regex tester: https://regex101.com/r/aqUtfn/2
EDIT: Another way to do it (I changed the answer by 'the fourth bird' around a bit to make it more strict) - (==|\?)((?<!\?)\s)?[[A-Za-z:;?!,."'][A-Za-z :;?!,."']+?\2?\1.
This will still match the stuff above (2 equal signs, a space, alphanumeric characters and punctuation, a space, and 2 more equal signs or a question mark, alphanumeric characters and punctuation, and another question mark), but uses backreferences to do it.
Link to online regex tester: https://regex101.com/r/aqUtfn/4