I am parsing below string value into OData query through java code.
objects.put("EndDate", "\/Date(1441756800)\/";
How can i parse the /Date(1441756800)/ into a string in java.
I have tried with below :
objects.put("EndDate", ""\\""//"Date(1441756800)""\\""//"";
throws error:(
I never used OData so I may not understand your question correctly, but if you are asking how to write \/Date(1441756800)\/ as String then you need to escape \ as it is String special character (used for instance when escaping or when creating other special characters like line separators \n).
So try with "\\/Date(1441756800)\\/"
Try this - objects.put("EndDate", "'Date(1441756800)'";
Related
I came to a scenario while using eclipse, in which if i use two back slash in below mention function.
"private Keywords(){
try{
OR=new Properties();
FileInputStream fs=new FileInputStream(System.getProperty("user.dir")+"**\\src\\com\\config\\OR.properties"**);
OR.load(fs);
"
this function works but if I use single slash it won't work . Is their way that i would be able to use single backward slash only while giving a source path..
Your question has nothing to do with Eclipse.
You need to escape back-slashes in Strings, as they are themselves an escape character.
What you can eventually use to somewhat "shorten" your code is the system property System.getProperty("file.separator"), then assign it to some constant and use that reference instead.
But that's close to cosmetics.
You can use the 2 backslashes as a single variable say,
String separator = "\\";
String file_path = "src"+separator +"com"+separator +"config"+separator +"OR.properties";
System.out.println("File Path is :: " + file_path);
Or as Mena Suggested, you can use:
String separator = System.getProperty("file.separator");
Get it straight with your String literals
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html
I'm trying to build a Java regex to search a .txt file for a Windows formatted file path, however, due to the file path containing literal backslashes, my regex is failing.
The .txt file contains the line:
C\Windows\SysWOW64\ntdll.dll
However, some of the filenames in the text file are formatted like this:
C\Windows\SysWOW64\ntdll.dll (some developer stuff here...)
So I'm unable to use String.equals
To match this line, I'm using the regex:
filename = "C\\Windows\\SysWOW64\\ntdll.dll"
read = BufferedReader.readLine();
if (Pattern.compile(Pattern.quote(filename), Pattern.CASE_INSENSITIVE).matcher(read).find()) {
I've tried escaping the literal backslashes, using the replace method, i.e:
filename.replace("\\", "\\\\");
However, this is failing to find, I'm guessing this is because I need to further escape the backslashes after the Pattern has been built, I'm thinking I might need to escape upto an additional four backslashes, i.e:
Pattern.replaceAll("\\\\", "\\\\\\\\");
However, each time I try, the pattern doesn't get matched. I'm certain it's a problem with the backslashes, but I'm not sure where to do the replacement, or if there's a better way of building the pattern.
I think the problem is further being compounded as the replaceAll method also uses a regex, with means the pattern will have it's own backslashes in there, to deal with the case insensitivity.
Any input or advice would be appreciated.
Thanks
Seems like you're attempting to to a direct comparison of String against another. For exact matches, you could do (
if (read.equalsIgnoreCase(filename)) {
of simply
if (read.startsWith(filename)) {
Try this :
While reading each line from the file, replace '\' by '\\'.
Then :
String lLine = "C\\Windows\\SysWOW64\\ntdll.dll";
Pattern lPattern = Pattern.compile("C\\\\Windows\\\\SysWOW64\\\\ntdll\\.dll");
Matcher lMatcher = lPattern.matcher(lLine);
if(lMatcher.find()) {
System.out.println(lMatcher.group());
}
lLine = "C\\Windows\\SysWOW64\\ntdll.dll (some developer stuff here...)";
lMatcher = lPattern.matcher(lLine);
if(lMatcher.find()) {
System.out.println(lMatcher.group());
}
The correct usage will be:
String filename = "C\\Windows\\SysWOW64\\ntdll.dll";
String file = filename.replace('\\', ' ');
I have a string.
String invalid = "backslash escaping as <>:;%+\/"."
I received an error message telling me to add \ to escape the sequence.
When I try to write this in Java I know that backslash needs to be escaped as \\. So I wrote it as:
String invalid = "backslash escaping as <>:;%+\\/\"."
Now this displays as backslash escaping as <>:;%+\\/".
The backslash is not escaping. How do I get only one backslash?
I don't find a problem with your modification. This runs as expected:
String invalid = "backslash escaping as <>:;%+\\/\".";
System.out.println(invalid);
output:
backslash escaping as <>:;%+\/".
In your first example, you have a little problem:
\/".
Which I believe should be like this:
/\".
Because in the first way, you are closing the string before you want to. The part ." is out of the String.
Edit:
try writing the output to a file or to a JTextField or something and see what happens, your string is correct and if you compare your output with my output it is the same. It might be an issue with your debugger (weird, but possible).
The second string in your post is correctly displayed. There must be something wrong with the way in which you observe the output.
Just try the simplest thing possible:
Create a file named Escape.java and write this code into its contents:
public class Escape {
public static void main(String... args) {
String s = "backslash escaping as <>:;%+\\/\".";
System.out.println(s);
}
}
Open a whatever command line that your OS provides and go to the folder with the said source file.
Compile the source file:
javac Escape.java
And run the class file:
java -cp . Escape
It should give this output:
backslash escaping as <>:;%+\/".
...which is exactly what you want, I believe.
I have a complete file path and I want to get the file name.
I am using the following instruction:
String[] splittedFileName = fileName.split(System.getProperty("file.separator"));
String simpleFileName = splittedFileName[splittedFileName.length-1];
But on Windows it gives:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
Can I avoid this exception? Is there a better way to do this?
The problem is that \ has to be escaped in order to use it as backslash within a regular expression. You should either use a splitting API which doesn't use regular expressions, or use Pattern.quote first:
// Alternative: use Pattern.quote(File.separator)
String pattern = Pattern.quote(System.getProperty("file.separator"));
String[] splittedFileName = fileName.split(pattern);
Or even better, use the File API for this:
File file = new File(fileName);
String simpleFileName = file.getName();
When you write a file name, you should use System.getProperty("file.separator").
When you read a file name, you could possibly have either the forward slash or the backward slash as a file separator.
You might want to try the following:
fileName = fileName.replace("\\", "/");
String[] splittedFileName = fileName.split("/"));
String simpleFileName = splittedFileName[splittedFileName.length-1];
First of all, for this specific problem I'd recommend using the java.util.File class instead of a regex.
That being said, the root of the problem you're running into is that the backslash character '\' signifies an escape sequence in Java regular expressions. What's happening is the regex parser is seeing the backslash and expecting there to be another character after it which would complete the escape sequence. The easiest way to get around this is to use the java.util.regex.Pattern.quote() method which will escape any special characters in the string you give it.
With this change your code becomes:
String splitRegex = Pattern.quote(System.getProperty("file.separator"));
String[] splittedFileName = fileName.split(splitRegex);
String simpleFileName = splittedFileName[splittedFileName.length-1];
Another simpler way could be to do
File f = new File(path);
String fileName = f.getName();
I believe this will work provided the paths are compatible with the platform, i.e. not sure if path "c:\file.txt" will work on Linux or not.
I have an XML which contains many special symbols like ® (HTML number ®) etc.
and HTML names like ã (HTML number ã) etc.
I am trying to replace these HTML symbols and HTML names with corresponding HTML number using Java. For this, I first converted XML file to string and then used replaceAll method as:
File fn = new File("myxmlfile.xml");
String content = FileUtils.readFileToString(fn);
content = content.replaceAll("®", "&\#174");
FileUtils.writeStringToFile(fn, content);
But this is not working.
Can anyone please tell how to do it.
Thanks !!!
The signature for the replaceAll method is:
public String replaceAll(String regex, String replacement)
You have to be careful that your first parameter is a valid regular expression. The Java Pattern class describes the constructs used in a Java regular expression.
Based on what I see in the Pattern class description, I don't see what's wrong with:
content = content.replaceAll("®", "&\#174");
You could try:
content = content.replaceAll("\\p(®)", "&\#174");
and see if that works better.
I don't think that \# is a valid escape sequence.
BTW, what's wrong with "®" ?
If you want HTML numbers try first escaping for XML.
Use EscapeUtils from Apache Commons Lang.
Java may have trouble dealing with it, so first I prefere to escape Java, and after that XML or HTML.
String escapedStr= StringEscapeUtils.escapeJava(yourString);
escapedStr= StringEscapeUtils.escapeXML(yourString);
escapedStr= StringEscapeUtils.escapeHTML(yourString);