How to regex a word that does not contain spaces? - java

I'd like to regex that a value has no spaces (^[\S]*$) and only contains letters (\w+). But how do I combine these two to one regex?
Expected result should be:
oneword: true
one word: false
1word: false

I guess you may want something like (if the empty string is to be valid):
^[A-Za-z]*$
\w contains digits and _ too, so that would match 1word.

For matching letters only this regex would be enough:
^[a-zA-Z]+$
\w means letter, digits and underscore

\w not only match alphabets, but also match digits, underscore(_). To match only alphabets, use:
^[A-Za-z]+$

If you want a non-regex solution, perhaps you could try something like this:
public boolean valid(final String string){
for(final char c : string.toCharArray())
if(!Character.isLetterOrDigit(c))
return false;
return true;
}

Use \p{Alpha} which is an alphabetic character:[\p{Lower}\p{Upper}]
With the above your regular expression will be \p{Alpha}+, which matches one or more of alphabetic characters. This ignores digits, underscores, whitespaces etc.
For more info look at section POSIX character classes (US-ASCII only) in this document.

Related

How to write a regex to match this String in Java?

I want to make a regular expression to the following string :
String s = "fdrt45B45"; // s is a password (just an example) with only
// letters and digits(must have letters and digits)
I tried with this pattern:
String pattern= "^(\\w{1,}|\\d{1,})$"; //but it doesn't work
I want to get a not match if my password doesn't contains letters or digits and a match if its contains both.
I know that: \w is a word character: [a-zA-Z_0-9] and \d is a digit: [0-9], but i can not mix \w and \d to get what i want. Any help or tips is very appreciated for a newbie.
A positive lookahead would do the trick :
String s = "fdrt45B45";
System.out.println(s.matches("(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z0-9]+"));
You should be able to use the following regex to achieve what you want. This uses a positive look ahead and will match any string containing at least one letter and at least one number.
^(?=.*\\d)(?=.*\\w).*

match whole sentence with regex

I'm trying to match sentences without capital letters with regex in Java:
"Hi this is a test" -> Shouldn't match
"hi thiS is a test" -> Shouldn't match
"hi this is a test" -> Should match
I've tried the following regex, but it also matches my second example ("hi, thiS is a test").
[a-z]+
It seems like it's only looking at the first word of the sentence.
Any help?
[a-z]+ will match if your string contains any lowercase letter.
If you want to make sure your string doesn't contain uppercase letters, you could use a negative character class: ^[^A-Z]+$
Be aware that this won't handle accentuated characters (like É) though.
To make this work, you can use Unicode properties: ^\P{Lu}+$
\P means is not in Unicode category, and Lu is the uppercase letter that has a lowercase variant category.
^[a-z ]+$
Try this.This will validate the right ones.
It's not matching because you haven't used a space in the match pattern, so your regex is only matching whole words with no spaces.
try something like ^[a-z ]+$ instead (notice the space is the square brackets) you can also use \s which is shorthand for 'whitespace characters' but this can also include things like line feeds and carriage returns so just be aware.
This pattern does the following:
^ matches the start of a string
[a-z ]+ matches any a-z character or a space, where 1 or more exists.
$ matches the end of the string.
I would actually advise against regex in this case, since you don't seem to employ extended characters.
Instead try to test as following:
myString.equals(myString.toLowerCase());

How to match all numerical characters and some single characters using regex

How can I match all numbers along with specific characters in a String using regex? I have this so far
if (!s.matches("[0-9]+")) return false;
I don't understand much regex, but this matches all characters from 0-9 and now I need to be able to match other specific characters, for example "/", ":", "$"
You can use this regex by including those symbols in a character class:
s.matches("[0-9$/:]+")
Read more about character class
You can add the other characters that you need to match to the end of the character group, like this:
if (!s.matches("[0-9/:$]+")) return false;
You need to be careful about several things:
If ^ is among the characters, it must not be the first one of the group
If - is among the characters, it must be the last one in the group
If ] is among the characters, it needs to be escaped for regex and for Java, e.g. [\\]]
If \ is among the characters, it needs to be escaped for regex and for Java, e.g. [\\\\]
Regex:
String regex = "\\d/:$+";

Java regex match all characters except

What is the correct syntax for matching all characters except specific ones.
For example I'd like to match everything but letters [A-Z] [a-z] and numbers [0-9].
I have
string.matches("[^[A-Z][a-z][0-9]]")
Is this incorrect?
Yes, you don't need nested [] like that. Use this instead:
"[^A-Za-z0-9]"
It's all one character class.
If you want to match anything but letters, you should have a look into Unicode properties.
\p{L} is any kind of letter from any language
Using an uppercase "P" instead it is the negation, so \P{L} would match anything that is not a letter.
\d or \p{Nd} is matching digits
So your expression in modern Unicode style would look like this
Either using a negated character class
[^\p{L}\p{Nd}]
or negated properties
[\P{L}\P{Nd}]
The next thing is, matches() matches the expression against the complete string, so your expression is only true with exactly one char in the string. So you would need to add a quantifier:
string.matches("[^\p{L}\p{Nd}]+")
returns true, when the complete string has only non alphanumerics and at least one of them.
Almost right. What you want is:
string.matches("[^A-Za-z0-9]")
Here's a good tutorial
string.matches("[^A-Za-z0-9]")
Lets say that you want to make sure that no Strings have the _ symbol in them, then you would simply use something like this.
Pattern pattern = Pattern.compile("_");
Matcher matcher = Pattern.matcher(stringName);
if(!matcher.find()){
System.out.println("Valid String");
}else{
System.out.println("Invalid String");
}
You can negate character classes:
"[^abc]" // matches any character except a, b, or c (negation).
"[^a-zA-Z0-9]" // matches non-alphanumeric characters.

split a string by any symbol

What is the regex that I should pass with String.split() in order to split the string by any symbol?
Now, by any symbol I mean any of the following:
`~`, `!`, `#`, `#`, ...
Basically any non-letter and non-digit printable character.
You should use a non word i.e \W
\W is inverse of \w
\W is similar to [^a-zA-Z0-9_] and so would match any non-word character except _
OR
you can simply use [^a-zA-Z0-9]
You can try using this: -
str.split("[^a-zA-Z0-9]");
This will not include an underscore.
\W is equivalent to: - "[a-zA-Z0-9_]"
You could either be specific like Spring.split("[~!#$]") or list the values you do not want to split upon Spring.split("[^\\w]")
You may want to use \W or ^\w. You may find more details here: Regex: Character classes
String str = "a#v$d!e";
String[] splitted = str.split("\\W");
System.out.println(splitted.length); //<--print 4
or
String str = "a#v$d!e";
String[] splitted = str.split("[^\\w]");
System.out.println(splitted.length); //<--print 4

Categories

Resources