This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Java - Regex problem
I have list of URLs of types:
http://www.example.com/pk/etc
http://www.example.com/pk/etc/
http://www.example.com/pk/etc/etc
where etc can be anything.
So I want to search only those URLs that contains www.example.com/pk/etc or www.example.com/pk/etc/.
Note: It is for all those who think that it is a duplicate question -- Kindly read both the questions carefully before marking this question as duplicate. Even after reading you can't understand that both the questions are different, then kindly leave without marking it as duplicate because I can't tell you the diff. in anymore detail
String pattern = "http://www.example.com/pk/[^/]+/?$";
I am assuming http://www.example.com/pk// is not accepted. If this should be accepted too, then use
String pattern = "http://www.example.com/pk/[^/]*/?$";
Your problem isn't fully defined so I can't give you an exact answer but this should be a start you can use:
^[^:]+://[^/]+\.com/pk/[^/]+/?$
The difference is that the / is no longer optional and there must be at least one more character after pk/.
These strings will match:
http://www.example.com/pk/ca
http://www.example.com/pk/ca/
https://www.example.com/pk/ca/
These strings won't match:
http://www.example.com/pk//
http://www.example.co.uk/pk/ca
http://www.example.com/pk
http://www.example.com/pk/
http://www.example.com/anthingcangoeshere/pk
http://www.example.com/pkisnotnecessaryhere
http://www.example.com/pk/ca/sf
So I want to search only those URLs that contains www.example.com/pk/etc or www.example.com/pk/etc/.
Update
I think this will work:
https?://.*\\.?[A-Za-z0-9]+\\.com/pk/etc/?[^.]
But every item in the list you gave contains what you are searching for.
Related
This question already has answers here:
What is the convention for word separator in Java package names?
(6 answers)
Closed 6 years ago.
For example:
Which of the following package names is correct?
com.google.payrolldivision;
or
com.google.payrollDivision;
Please just answer the question without beating around the bush?
Please just answer the question
OK, then taking your question as a "a xor b", the answer is
com.google.payrolldivision;
as per https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html.
However, your title and post ask two very different questions, so it's hard to "just answer the question".
According to the Oracle webpage, you should write your package name in lowercase.
Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a little/huge problem with String comparison in Java, I want to compare two Strings and .equals([...]) does not give me the correct result.
I also tried the following: ==, .compareTo([...]), .trim(), .equalsIgnoreCase([...]), creating a Collator with default Locale and using [collator].compare.
(All fail to work)
The first String comes from an already created object (the content of the string is from a database), the second String comes from a newly created object (but has been passed to a method), the content of this String is from the same database.
I am pretty clueless what to do now, the last thing I'd try is to convert it to some number (i.e. hex).
I already tried to write both Strings to console and manually look for differences, but there is none...
Code is this:
public static Lagerplatz hinzufuegen(Lagerplatz lagerplatz) {
boolean neu = true;
if (lagerplaetze.isEmpty()) {
lagerplaetze.add(lagerplatz);
System.out.println(lagerplatz.getBezeichnung() + " erstmalig hinzugefügt!");
}
for (int i = 0; i < lagerplaetze.size(); i++) {
System.out.println("'" + lagerplatz.getBezeichnung() + "'" + " - " + "'"
+ lagerplaetze.get(i).getBezeichnung() + "'");
if (lagerplatz.getBezeichnung().equalsIgnoreCase(lagerplaetze.get(i).getBezeichnung())) {
neu = false;
}
}
if (neu) {
lagerplaetze.add(lagerplatz);
System.out.println(lagerplatz.getBezeichnung() + " hinzugefügt!");
}
return lagerplatz;
}
The if-part with (lagerplaetze.isEmpty()) does work, after the first one is added it should check if the Lagerplatz (at least the name of it) is already in the lagerplaetze-ArrayList, if so then don't add, if not add.
Stepping through it revealed that the objects are correctly referenced...
Thanks very much in advance, and sorry if this question has been answered already but I can't find a helping answer under all these questions...
EDIT1: Normalizing does not help in this case, "umlaute" (german ä, ö or ü) are not causing the problem...
Converting the String to a byte[] and converting this byte-array to String like:
(Arrays.toString(lagerplatz.getBezeichnung().getBytes()).equalsIgnoreCase(
Arrays.toString(lagerplaetze.get(i).getBezeichnung().getBytes())))
and then comparing it does also not solve the problem, the bytes of the two strings are exactly the same: '[72, 55, 48]' - '[72, 55, 48]'
EDIT2: The problem is not with the String comparison, it is because the variables of the class "Lagerplatz" are static, they were replaced each time the loop is entered...
Maybe delete this question?
The only time two strings that look exactly the same but equals() does not result in true is when the unicode composition is different.
For example one can compose the A umlaut (Ä) with a single character: \u00C4
Or with a combination of the A character and the dots (the dieresis character ¨): \u0041\u0308
In essence, you are using two unicode characters for one letter. Because equals() compares characters, the form with dieresis is not equals to the form without.
To overcome this problem, one must decompose each string to a canonical form before comparison.
In Java one can create such a canonical form like this:
java.text.Normalizer.normalize("Your String", java.text.Normalizer.Form.NFD);
Once normalized, equals() will work as expected.
Obviously, since you didn't provide any data, this answer may or may not match your problem.
In any case, you might want to normalize all Strings in some form and then use a Set as data structure, not a list.
This question already has answers here:
What is the role of the data types inside of < > in Java? [duplicate]
(5 answers)
Closed 8 years ago.
I am familiar with using parentheses, as in myMethod(myParameter), in Java and other programming languages, but what do the lesser than < and greater than > signs mean when they are used together in a array name? Is there a special name for them?
Sorry if this is a duplicate, but I don't know how to search for this.
Clarification: I wasn't referring to the role of any specific type inside the symbols, but rather the usage of the <> symbols themselves.
it is java generics, for type safe, and always.using with collections.
i suggest you read the SCJP book, it has a chapter called Generics and Collections, it provides all details you will need, it really helpful. hope it helps.
This question already has answers here:
How to convert number to words in java
(31 answers)
Closed 9 years ago.
I'm having a list of Integer from 1 to 100. If I loop through the list, I wanted to make the output as,
"One"
"Two" .....
"Hundred"
Is there any direct method in Java to obtain the above output?
No such method or class has been provided by JDK.
You can use the code mentioned here or here for reference purpose.
switch case are used to meet that requirement: Here
is source code.
Answer of this question described here: How to convert number to words in java
Officially this is not possible or no standard library available by native Java.
Don't duplicate.
There is none in the official Java libraries. However, the International Components for Unicode project has a RuleBasedNumberFormat with those capabilities. It even has a SPELLOUT constant.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
I want the pattern for removing the &b=128&f=norefer from following url
http://www.yahoo.com/url/5f&b=52&f=norefer
http://www.yahoo.com/url/6aa82d?show=all&page=2&b=52
String finalUrl =decodedUrl.replace("&b=52","");
page.setPageUrl(finalUrl);
I want to remove &b=52&f=norefer from the first url and &b=52 and from the second url which pattern i will use please give me the code without hard-coded value.
url.replaceFirst("&b=.*", ""); will remove the tail of the string starting from "&b=". I hope it is good enough for you. At least it is OK for your two exampels and is not sensitive to value of parameter b.
If it is not enough try to describe your task more specifically or just learn a little bit regular expressions.