Java Properties File to Use / Forward Slash in Key - java

I have a properties file, and I need to use the / forward slash in some of my keys.
e.g.
app.module/hdr.key1=value 1
app.module/hdr.key2=value 2
I just have no choice but need to do it that way. Please advise is this achievable and how to do this?
Thanks.

The use of forward slashes will not cause a problem. To understand why, I suggest you read a critique of the syntax used in Java properties that I wrote. In essence, what you need to know is the following:
Leaving aside edge cases (comment lines, blank lines and escape sequences), the syntax of a name=value pair permits almost any character (including forward-slashes) in the name.
The = can actually be any of the following: (1) = (optionally preceded and/or followed by whitespace); (2) : (optionally preceded and/or followed by whitespace); or (3) just whitespace. So, yes name=value is equivalent to name:value and also to name value.
All escape sequences begin with the backslash character. For details of the escape sequences, I suggest you do a Google search for java.util.Properties to find online documentation for that class, and look at the long description of the load(InputStream) method.

Related

How to write a regular expression that can matches the whole function #Prompt (…) whatever written inside () even if it contains another ()

For example I want replace any prompt function in an SQL query
I have used this expression
Query = Query.replaceAll("#prompt\\s*\\(.*?\\)", "(1)");
This expression works in this example
#Prompt('Bill Cycle','A','MIGRATION\BC',,,)
#Prompt('Bill Cycle','A','MIGRATION\BC',,,)
and the output is is (1)
but when it does not work on this example
#Prompt('Groups','A','[Lookup] Price Group (2)\Nested Group',,,)
the out put is (1) \Nested Group',,,) which is not valid
Sadly, as pointed out by Joe C in a comment, what you are trying to do cannot be done in a regular expression for arbitrary depth parenthesis. The reason is because regular-expressions are not capable of "counting". You need a stack machine for that, or a context-free language parser.
However, you also suggest that the 'prompted' content is always inside single quotes. I assume below the standard Java regexp library. Other regexp libraries might need translation...
"#Prompt\\('[^']*'(\s*,\s*(('[^']*')|([^',)]*)))*\\)"
So, you are searching within prompt for blocks of single-quoted text. The search assumes that each internal bit of content is enclosed in single quotes.
Verify at https://regex101.com/r/nByy0Y/1 (I made a couple fixes). Note that at regex101.com, it will treat the double back-slash as intending a literal back-slash. What you want instead is just to quote the parenthesis so that you want a literal parenthesis.
Because you are using the lazy quantifier '?', it is stopping the match at the end of the first ')'. removing that will let it go to the end greedily, as such:
#prompt\(.*\)
But if there is concern that the entries may have more parans after the one in question, it will cause problems.
Assuming the additional parens will always be in quotes, you can do this:
#prompt\((('([^'])*',*)*|(.*,*)*)\)
Here is it looking for items wrapped in single quotes OR text without parens, which should capture all of the single quoted elements or null params or unquoted text params

Using Resourcebundle Properties in Javascript

I work on a Java EE web application that uses a combination of Dojo and plain javascript for the front-end.
We've discovered that when ResourceBundle properties are used in javascript, in some cases they end up breaking code.
Specifically, this happens when the properties contain quotes (single and double) & escape sequences (\n, \s ...).
The solution seems to be to include extra escape characters. For instance, \n needs to be prepended by one more slash (\\n) when used in a Js alert
to correctly render the line break, and Quotes if not escaped truncate the content prematurely for obvious reasons.
Our solution to the above issues so far has been to put in the extra escape characters in the property files itself. But this is something that we would like to move away from.
It seems like this might be a widespread problem and I'd like to hear from the experts on how you might have solved this problem.
Current Usage: key=A newline is represented with \\n and this \" is within quotes \".
Envisioned Usage : key=A newline is represented with \n and this " is within quotes ".
PS: We typically use the <fmt:message> tag to access these values in the front end and for use in javascript.
Consider using StringUtils. If has a method to escape input like yours.
http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringEscapeUtils.html#escapeJava(java.lang.String)

Java String#contains() using String#matches() with escape character

I need a simple way to implement the contains function using matches. I believe this is my starting point:
xxx.matches("'.*yyy.*'");
But I need to make it a universal method and pre-process whatever I search for to be accepted by matches! This must be done using only the escape '\' character!
Imagine a string SEARCH_FOR that can contain some special characters that must be "regex escaped"...
String SEARCH_FOR="*.\\"
xxx.matches("'.*" + SEARCH_FOR + ".*'");
Are there any catches? Special situations? Any other "special chars should be taken into account?
Are you looking for Pattern.quote(String) ?
This escapes special characters for you.
EDIT:
After reading the comments, I really hope you try Pattern.quote(yourString.toLowerCase()) as it sounds like you've been using Pattern.quote(yourString).toLowerCase(). If DataNucleus is applying the regex then there should be no problems with using the \Q and \E escape sequence.
Since you have really asked for it, ".\\".replaceAll("(\\.|\\$|\\+|\\*|\\\\)", "\\\\\$1") outputs \.\\
This will escape .'s, $'s, + 's, *'s and \'s. Note that the security of this is now all upon you. If you don't escape something you needed to, or you escape it incorrectly, you will either allow people to use regex inside the search term when you weren't expecting to or it won't returns results that you were expecting.

How do I swap the left hand and right hand sides of a C/C++ assignment statement in gvim?

To be honest, I actually have a solution for this, but Google search finds so many great tips for me from this site, that I had to contribute something back. Here is what I came up with. For a single line:
s/^\(\s\+\)\(.*\) = \(.*\);/\1\3 = \2;/
For multiple lines starting at the current line, add .,.+<line count>. For example:
.,.+28s/^\(\s\+\)\(.*\) = \(.*\);/\1\3 = \2;/
will substitute on the current line and the following 28 lines. This should also work for Java and Perl. For Python, omit the ending semicolon from the pattern and substitution (unless you're the sort who uses the optional semicolon).
After typing all that, I find I do have a question. Is there a way to simplify it so I don't have so many escape characters?
Use 'very magic': add \v to the expression. See :help magic. Basically, it mean that all non-alphanumeric characters have special (i.e. regular expression operator meanings) unless escaped, which means that they do not need to be escaped in your usage above.
Using \v at the start of your regex can help make it more readable. \v means "very magic", that all characters have are special except those in the sets '0'-'9', 'a'-'z', 'A'-'Z' and '_'.
So your first example could be converted like so:
s/\v^(\s+)(.*) \= (.*)\;/\1\3 = \2\;/
The = and the ; now need to be escaped to identify them as literals but all the other high-ASCII chars don't.

Replace the property value using RegEx

I have config.properties file containing properties in Java Properties format.
I need to replace the value of a property with a known name with a new value. The comments and formatting of the file should be preserved.
My current approach is to use RegEx to match the property name and then replace its value. However, Java Properties supports multi-line values, which I have hard time matching.
Here is an example. Suppose config.properties contains the following text:
# A property
A = 1\
2
# B property
B = 2
I would like to replace the value of property A with "3". The end result should be:
# A property
A = 3
# B property
B = 2
My current RegEx (?s)(A[\\s]*=[\\s]*)(.*) does not work correctly.
Please suggest a RegEx or an a different way of doing this.
Thanks!
Try this:
String regex = "(?m)^(A\\s*+=\\s*+)"
+ "(?:[^\r\n\\\\]++|\\\\(?:\r?\n|\r|.))*+$";
I left the first part as you wrote it so I could concentrate on matching the value; the rules governing the key and separator are actually much more complicated than that.
The value consists of zero or more of any character except carriage return, linefeed or backslash, or a backslash followed by a line separator or any single non-line-separator character. The line separator can be any of the three most common forms: DOS/Windows (\r\n), Unix/Linux/OSX (\n) or pre-OSX Mac (\r).
Note that the regex is in multiline mode so the line anchors will work, but not singleline (DOTALL) mode. I also used possessive quantifiers throughout because I know backtracking will never be useful.
You have tools in Java to load, read, modify and save properties files.
Personally I like Jakarta Commons Configuration.
I agree with streetpc on using Jakarta Commons Configuration.
However to focus on your regex, the problem is that most of the regex engines work on a line per line basis by default.
For example in the (quite old) Perl5Util class (see http://jakarta.apache.org/oro/api/org/apache/oro/text/perl/Perl5Util.html) you can read that patterns have following syntax :
[m]/pattern/[i][m][s][x]
The m prefix is optional and the meaning of the optional trailing options are:
i case insensitive match
m treat the input as consisting of multiple lines
s treat the input as consisting of a single line
x enable extended expression syntax incorporating whitespace and comments

Categories