Java - method equals and more possible letters - java

I am doing a cipher program, so I am replacing letters with other letters.
Here comes my problem:
I need to replace ONLY letter, not special symbols so I am checking if selected character is letter or not. I need to use equals method with more possible letters to get "true"
I have:
if(pismenka[i].equals(["abcdefghijklmnopqrstuvwxyz"]))
but it doesn't work at all, it was only idea. Do I have to use || symbol or there is any more clear solution?
Thank you, AliFox.

For identifying characters you can use the following (Java Character):
Character.isLetter(<target_char>)
And if replacement is to be done, following would help you replace characters from a-z:
<target_string>.replaceAll("[a-zA-Z]",<replacement>)

Use the String's replaceAll method, that takes a regex as a first argument.
Assuming you want to replace all non-special characters with a String replacement, the command would be:
pismenka[i].replaceAll("[a-z]",replacement);
Then, you don't need another explicit check if your String matches the regex. It is done inside this method. If your String does not contain any non-special characters, it is left intact.

Character#equals(Object) checks whether or not your character is equal to the array (not to any its element). They are obviously not equal.
If you need to check that your character is a lower-case latin then use this check
c >= 'a' && c <= 'z'.

Related

Why doesn't [[a-z]*&&[^a]] catch "bc", but "b"?

Ok, so I have tried to become more familiar with the intersection in regex (&&).
On the java.util.Pattern page all the regex are explained and && is only ever used next to a range (like [a-z&&[^e]]). But I tried to use it like this: [[a-z]*&&[^a]]. To me it seemed logical that this would match all lower case strings, expect the string "a", but instead it seems to be equivalent with [a-z&&[^a]].
So the actual question is: Where did the * operator go? How does this only catch single character strings?
I think your approach is wrong to use an intersection: To match all lowercase strings except "a":
^(?!a$)[a-z]+$
And you can drop the wrapping ^ and $ when calling matches()"
if (input.matches("(?!a$)[a-z]+")) {
// it's an all-lowercase string, but not "a"
}
Of course you don't need regex. although it's a little long winded:
if (input.equals(input.toLowerCase()) && !input.equals("a"))
but you can read it more easily.
Inside a character class (marked by []) the * character has no special meaning. It simply represents the character itself.
So the regular expression
[[a-z]*&&[^a]]
allows exactly one character being one of the following:
b, c, d, ..., z, *
The [a-z] and the following * are unioned, and the resulting character class is intersected with [^a] which simply removes the a character.
Valid strings are (for example):
b
*
c
But
a
is not, as well as each string that contains more than one character.
Now to the solution for what you want. You want to have strings (allowing more than one character, I assume) that could also contain the letter 'a' but not the string "a" alone. The easiest is a group that does this distinction:
(?!a$)[a-z]*
The group (?!a$) is called a zero-width negative lookahead. It means that the looked at character is not consumed (zero-width), and it is not allowed (negative). The '$' character looks till the end. Otherwise, words beginning with 'a' would also be rejected.
Character Class Intersection is supported in Java. The problem is that inside a character class, * looses its special meaning and the literal star "*" will be matched instead. Your regex should be:
[a-z&&[^a]]*
Now it'll match all characters in the range "a-z" except the "a" character.
Example:
Pattern p = Pattern.compile("[a-z&&[^a]]");
Matcher m = p.matcher("a");
System.out.println(m.matches()); // false
Try to use * outside of class:
[[a-z]&&[^a]]*
Interception of two character classes gives you another character class.
And as said in other answers, * doesn't mean quantity inside class. So, use it outside.

Regular expression ([A-Za-z]*) matches digits and special characters?

i need to validate a text fiel in my application. this cant contain neither digit nor special char so i tried this regex:
[A-Za-z]*
The problem is that this regex doesn't work when i put a digit or a special char in the middle or at the end of the String.
You should use it like this:
^[A-Za-z]+$
to match text (1 or more in length) containing ASCII letters only.
Go ahead and try ^[A-Za-z]*$ instead.
You could use the following Regex:
Pattern p = Pattern.compile("^[A-Za-z]*$");
See a list of regex-specs on http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
The pattern you describe will never work. Without the begin and end bonds the pattern will look for a substring that matches. Since an empty string is also allowed (star means 0 or more characters), one can simply use the empty string anywhere.
you are check help me validate date time type YYYY-MM-DDThh:mm:ss.sssZ or
YYYY-MM-DD
^(((19|20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01]))|(((19|20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])T([01]?[0-9])|([2][0123]):([012345]?[0-9]):([012345]?[0-9])\.([0-9][0-9][0-9][Z]))$

How can I add an underscore before each capital letter inside a Java String?

I have a string like this "HelloWorldMyNameIsCarl" and I want it to become something like "Hello_World_My_Name_Is_Carl". How can I do this?
Yes, regular expressions can do that for you:
"HelloWorldMyNameIsCarl".replaceAll("(.)([A-Z])", "$1_$2")
The expression [A-Z] will match every upper case letter and put it into the second group. You need the first group . to avoid replacing the first 'H'.
As Piligrim pointed out, this solution does not work for arbitrary languages. To catch any uppercase letter defined by the Unicode stardard we need the Unicode 4.1 subproperty \p{Lu} which matches all uppercase letters. So the more general solution looks like
"HelloWorldMyNameIsCarl".replaceAll("(.)(\\p{Lu})", "$1_$2")
Thanks Piligrim.
Is this homework? To get you started:
Create a StringBuffer
Iterate over your string.
Check each character to be uppercase (java.lang.Character class will help)
Append underscore to buffer if so.
Append current character to buffer.
Here's a hint to get you thinking along a possible solution:
Find a way of splitting the string into parts at each capital letter
Join the split strings back up with underscores between them
Useful keywords:
split
regular expression/regex

Java String split regex not working as expected

The following Java code will print "0". I would expect that this would print "4". According to the Java API String.split "Splits this string around matches of the given regular expression". And from the linked regular expression documentation:
Predefined character classes
. Any character (may or may not match line terminators)
Therefore I would expect "Test" to be split on each character. I am clearly misunderstanding something.
System.out.println("Test".split(".").length); //0
You're right: it is split on each character. However, the "split" character is not returned by this function, hence the resulting 0.
The important part in the javadoc: "Trailing empty strings are therefore not included in the resulting array. "
I think you want "Test".split(".", -1).length(), but this will return 5 and not 4 (there are 5 ''spaces'': one before T, one between T and e, others for e-s, s-t, and the last one after the final t.)
You have to use two backslashes and it should work fine:
Here is example:
String[] parts = string.split("\\.",-1);
Everything is ok. "Test" is split on each character, and so between them there is no character. If you want iterate your string over each character you can use charAt and length methods.

Simple java regexp problem

Why this code would fail?
assertTrue(Pattern.matches("[^a-zA-Z0-9]", "abc;"));
Because the .matches() method tries to match the entire string, and your regex doesn't match the entire string, only the semicolon. The Matcher.find() method would work (in this case: find a character that is not a letter between a and z nor a number between 0 and 9. Of course, it will also find á, ö etc.)
What is it you really want to do?
If fails because matches tries to match the complete string, your regexp matches 1 character which is not in the character ranges you list, if you change to:
assertTrue(Pattern.compile("[^a-zA-Z0-9]").matcher("abc;").find());
it should assert true.
Because Pattern.matches() is equivalent to the corresponding pattern being compiled and fed to Matcher.matches() which, as specified, checks to see if the entire input matches the pattern. If you only want to match part of the input, you'll want to use Matcher.find() instead.
try "^[a-zA-Z0-9]" as pattern
Because when you put the ^ inside, it means any char not in a-z or A-Z. What you want is ^[a-zA-Z0-9]

Categories