How to unescape a Java string literal in Java?

The Problem The org.apache.commons.lang.StringEscapeUtils.unescapeJava() given here as another answer is really very little help at all. It forgets about \0 for null. It doesn’t handle octal at all. It can’t handle the sorts of escapes admitted by the java.util.regex.Pattern.compile() and everything that uses it, including \a, \e, and especially \cX. It has no support for … Read more

How to use a variable inside a regular expression?

From python 3.6 on you can also use Literal String Interpolation, “f-strings”. In your particular case the solution would be: if re.search(rf”\b(?=\w){TEXTO}\b(?!\w)”, subject, re.IGNORECASE): …do something EDIT: Since there have been some questions in the comment on how to deal with special characters I’d like to extend my answer: raw strings (‘r’): One of the … Read more

Java – escape string to prevent SQL injection

PreparedStatements are the way to go, because they make SQL injection impossible. Here’s a simple example taking the user’s input as the parameters: public insertUser(String name, String email) { Connection conn = null; PreparedStatement stmt = null; try { conn = setupTheDatabaseConnectionSomehow(); stmt = conn.prepareStatement(“INSERT INTO person (name, email) values (?, ?)”); stmt.setString(1, name); stmt.setString(2, … Read more

Process escape sequences in a string in Python

The correct thing to do is use the ‘string-escape’ code to decode the string. >>> myString = “spam\\neggs” >>> decoded_string = bytes(myString, “utf-8”).decode(“unicode_escape”) # python3 >>> decoded_string = myString.decode(‘string_escape’) # python2 >>> print(decoded_string) spam eggs Don’t use the AST or eval. Using the string codecs is much safer.

How to decode Unicode escape sequences like “\u00ed” to proper UTF-8 encoded characters?

Try this: $str = preg_replace_callback(‘/\\\\u([0-9a-fA-F]{4})/’, function ($match) { return mb_convert_encoding(pack(‘H*’, $match[1]), ‘UTF-8’, ‘UCS-2BE’); }, $str); In case it’s UTF-16 based C/C++/Java/Json-style: $str = preg_replace_callback(‘/\\\\u([0-9a-fA-F]{4})/’, function ($match) { return mb_convert_encoding(pack(‘H*’, $match[1]), ‘UTF-8’, ‘UTF-16BE’); }, $str);

How to print a single backslash?

You need to escape your backslash by preceding it with, yes, another backslash: print(“\\”) And for versions prior to Python 3: print “\\” The \ character is called an escape character, which interprets the character following it differently. For example, n by itself is simply a letter, but when you precede it with a backslash, … Read more

Saving utf-8 texts with json.dumps as UTF8, not as \u escape sequence

Use the ensure_ascii=False switch to json.dumps(), then encode the value to UTF-8 manually: >>> json_string = json.dumps(“ברי צקלה”, ensure_ascii=False).encode(‘utf8’) >>> json_string b'”\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94″‘ >>> print(json_string.decode()) “ברי צקלה” If you are writing to a file, just use json.dump() and leave it to the file object to encode: with open(‘filename’, ‘w’, encoding=’utf8′) as json_file: json.dump(“ברי צקלה”, json_file, … Read more

Escaping HTML strings with jQuery

There is also the solution from mustache.js var entityMap = { ‘&’: ‘&amp;’, ‘<‘: ‘&lt;’, ‘>’: ‘&gt;’, ‘”‘: ‘&quot;’, “‘”: ‘&#39;’, “https://stackoverflow.com/”: ‘&#x2F;’, ‘`’: ‘&#x60;’, ‘=’: ‘&#x3D;’ }; function escapeHtml (string) { return String(string).replace(/[&<>”‘`=\/]/g, function (s) { return entityMap[s]; }); }