Java string inside string to string - java

I have this string: "\"Blah \'Blah\' Blah\"". There is another string inside it. How do I convert that into: Blah 'Blah' Blah? (you see, unescaping the string.) This is because I get a SQL Where query:
WHERE blah="Blah \'Blah\' Blah"
When I parse this, I get the string above (still inside quotes and escaped.) How would I extract that, un-escaping the string? Or is ther some much easier way to do this? Thanks,
Isaac

DO NOT DO THIS.
Follow the proper steps for parametrization of a query on your Database/Platform, and you won't have to escape anything. You also will protect yourself from injection vulnerabilities.

Put the string in a property file, Java supports XML property files and the quote character does not need to be escaped in XML.
Use loadFromXML(InputStream in) method of the Properties class.
You can then use the MessageFormat class to interpolate values into the String if needed.

This should be about right. This assumes that if it starts with a quote, it ends with a quote.
if (val.startsWith("\"") || val.startsWith("\'"))
val = val.substring(1, val.length-2);
You may wish to add val = val.trim(); as well.

"\"Blah \'Blah\' Blah\"".replaceAll("\"", "")

Related

How to replace xml empty tags using regex

I have a lot of empty xml tags which needs to be removed from string.
String dealData = dealDataWriter.toString();
someData = someData.replaceAll("<somerandomField1/>", "");
someData = someData.replaceAll("<somerandomField2/>", "");
someData = someData.replaceAll("<somerandomField3/>", "");
someData = someData.replaceAll("<somerandomField4/>", "");
This uses a lot of string operations which is not efficient, what can be better ways to avoid these operations.
I would not suggest to use Regex when operating on HTML/XML... but for a simple case like yours maybe it is ok to use a rule like this one:
someData.replaceAll("<\\w+?\\/>", "");
Test: link
If you want to consider also the optional spaces before and after the tag names:
someData.replaceAll("<\\s*\\w+?\\s*\\/>", "");
Test: link
Try the following code, You can remove all the tag which does not have any space in it.
someData.replaceAll("<\w+/>","");
Alternatively to using regex or string matching, you can use an xml parser to find empty tags and remove them.
See the answers given over here: Java Remove empty XML tags
If you like to remove <tagA></tagA> and also <tagB/> you can use following regex. Please note that \1 is used to back reference matching group.
// identifies empty tag i.e <tag1></tag> or <tag/>
// it also supports the possibilities of white spaces around or within the tag. however tags with whitespace as value will not match.
private static final String EMPTY_VALUED_TAG_REGEX = "\\s*<\\s*(\\w+)\\s*></\\s*\\1\\s*>|\\s*<\\s*\\w+\\s*/\\s*>";
Run the code on ideone

(How) can I split a long string in a maven property without having newline characters in the property value?

I tried it with backslash-escaping the newline, like this:
<my.property>MIICQDCCAakCBEeNB0swDQYJKoZIhvcNAQEEBQAwZz\
ELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNh</my.property>
or this:
<my.property>\
MIICQDCCAakCBEeNB0swDQYJKoZIhvcNAQEEBQ\
AwZzELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNh\
</my.property>
But it didn't work, I get the backslash and the newline in the property value. Is it possible to get the value of "my.property" be this:
MIICQDCCAakCBEeNB0swDQYJKoZIhvcNAQEEBQAwZzELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNh
without having to write the whole String on one line?
No, there is no way because the Plexus XML reader does not support that type of continuation you are used from the shell.
Break the string into multiple properties and use all into one final property like below. It is a hack but it works.
<Properties>
<p1>string1</p1>
<p2>string2</p2>
<p3>string3</p3>
<p>${p1}${p2}${p3}</p>
</Properties>
try using shift+enter if getting the values to span across multiple lines is your only aim.

Typesafe ConfigFactory error with reserved characters

Hi I am trying to load configuration from a String in Java as follows:
#Test
public void testIllegalCharacter(){
String input = "prop=\\asd";
Config conf = ConfigFactory.parseString(input);
}
The code above produces the following error:
com.typesafe.config.ConfigException$Parse: String: 1: Expecting a value but got wrong token: '\' (Reserved character '\' is not allowed outside quotes) (if you intended '\' (Reserved character '\' is not allowed outside quotes) to be part of a key or string value, try enclosing the key or value in double quotes, or you may be able to rename the file .properties rather than .conf)
I understand I have an illegal character in my String. Although how do I find the full set of illegal characters?
If I (for example) convert this String into a Properties object and then parse it with ConfigFactory.parseProperties I can see the value "\\asd" in resolved as "asd". So there must be some some sanitising going on in the typesafe library, I wish I could call that sanitisation myself, but I cannot see how. Parsing to Properties is not a viable solution as the configuration could be composed by Objects or Lists too.
Has anyone have any suggestion how to solve this issue?
Alternatively can someone point out all the reserved characters set?
Many thanks
If I understand the error message correctly, you should put quotes around you special characters, e.g. like this:
"prop=\"\\asd\"";
Not sure why you's want a property definition with a backslash a ('\a') in it, but I guess I don't need to know :-)
I think I might have found the answer. I need to set the ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES)
Which works for the test below:
#Test
public void test(){
String input = "prop=C:/MyDocuments/mydir";
Config conf = ConfigFactory.parseString(input, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES));
assertEquals("C:/MyDocuments/mydir", conf.getAnyRef("prop"));
}
But will not work for the test with backslashes
#Test
public void test(){
String input = "prop=C:\\MyDocuments\\mydir";
Config conf = ConfigFactory.parseString(input, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES));
assertEquals("C:\\MyDocuments\\mydir", conf.getAnyRef("prop"));
}
Which fails:
org.junit.ComparisonFailure:
Expected :C:\MyDocuments\mydir
Actual :C:MyDocumentsmydir
So I am not sure this is the definitive answer...

Java - Regex String replacement

I want to replace some parts of a String with Regex. It's the 192001Z part of the string I want to replace.
Code:
String met = "192001Z 17006KT 150V210 CAVOK 11/07 Q1004 NOSIG";
String regexZ = "[0-9].{5}Z";
met = met.replaceAll(regexZ, "${.now?string(\"ddHHmm\")}Z");
I get an error when I want to replace a part of the String with ${.now?string(\"ddHHmm\")}Z.
But when I e.g. replace ${.now?string(\"ddHHmm\")}Z with ThisNeedsToBeReplaced everything works just fine. So my guess is that something is wrong with the string I want to use to replace parts of my original string (met).
The error I receive is Illegal group reference.
Does anyone have an idea what's wrong with ${.now?string(\"ddHHmm\")}Z?
You need to use:
met = met.replaceAll("\\b\\d{6}Z\\b", "\\${.now?string(\"ddHHmm\")}Z");
Correct regex to match 192001Z is \b\d{6}Z\b
You need to escape $ in replacement as well otherwise it is considered a back reference e.g. $1, $2 etx.

cannot parse String with Java Regex

I have a string formatted as below:
source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..
It's a path, the number in () is the weight for the edge, I tried to split it using java Pattern as following:
[a-zA-Z.0-9]+-{1}({1}\\d+){1}
[a-zA-Z_]+.[a-zA-Z_]+.(\\d)+-(\\d+)
[a-zA-Z.0-9]+-{1}({1}\\d+){1}-{1}>{1}
hopefully it split the string into fields like
source1.type1.8371-(12345)
source2.type3.3281-(38270)
..
but none of them work, it always return the whole string as the field.
It looks like you just want String.split("->") (javadoc). This splits on the symbol -> and returns an array containing the parts between ->.
String str = "source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..";
for(String s : str.split("->")){
System.out.println(s);
}
Output
source1.type1.8371-(12345)
source2.type3.3281-(38270)
source4.type2.903..
It seems to me like you want to split at the ->'s. So you could use something like str.split("->") If you were more specific about why you need this maybe we could understand why you were trying to use those complicated regexes

Categories

Resources