I am new in Jython.. I want to use the string formatting str.format() in Jython (look here).
If I try to use it like this:
testText = 'This is a {word}'
and later on
str.format(testText, word='test!')
the exception text is:
Error: exceptions.AttributeError instance of 'org.python.core.PyReflectedFunction' has no attribute 'format'
What do I have to add in the imports/classpath or anywhere?!
Thanks for your help!!
Kind regards
Try this:
print "%(map_1)s %(map_2)s" % {'map_1':'Hello','map_2':'World'}
Related
I want to replace Encode::forUri with something that is not depricated. Does anyone know what method should I use to cover that up? I can't use forUriComponent() because I have to convert an link, not a query, and also forUriComponent() doesn't convert all the characters as forUri() did
Code example:
private static final ImmutableList<UnaryOperator> ENCODING_CHAIN_URL_IN_HTML_ATTRIBUTE = ImmutableList.of(Encode::forUri, EsEncode::uriAsSafeSchemeUri, Encode::forHtml);
Thanks!
I think you should use
URLEncoder.encode("NAME", "UTF-8");
https://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html#encode-java.lang.String-java.lang.String-
Example
URLEncoder.encode(
"urlStringParams",
java.nio.charset.StandardCharsets.UTF_8.toString()
)
I am working with some legacy code that has a static method call which we need to remove from our source tree.
The existing code is as follows:
Logger.getInstance(JdkUtil.forceInit(SomeBusiness.class));
What we need to end up with is:
Logger.getInstance(SomeBusiness.class);
I've spent all day today trying to figure out how to do that replacement. Since I have very little experience with regular expressions, I have only been able to come up with a pattern that matches the source string.
The pattern JdkUtil.forceInit([a-zA-Z_0-9]*.class) finds matches on the input string I am providing. I've tested this at https://www.freeformatter.com/java-regex-tester.html
So if anyone can post a Java solution to this, I would really appreciate it.
Below is some Groovy code that I have so far. What I am missing is to how correctly replacement explained above.
String source = 'Logger.getInstance(JdkUtil.forceInit(RtpRuleEngineCompiledImpl.class))'
String regexpPattern = 'JdkUtil.forceInit\\([a-zA-Z_0-9\\)]*.class\\)'
String replaced = source.replaceFirst(regexpPattern, 'hello')
println replaced
When I run the above code I get the following output:
Logger.getInstance(hello)
Obviously 'hello' is just for testing.
Thanks in advance to anyone who can give me some suggestions.
You'll likely want to do something such as:
class StackOverflow {
public static void main(String[] args) {
String source = "Logger.getInstance(JdkUtil.forceInit(RtpRuleEngineCompiledImpl.class))";
String regexpPattern = "JdkUtil.forceInit\\(([a-zA-Z_0-9]*.class)\\)";
String replaced = source.replaceFirst(regexpPattern, "$1");
System.out.println(replaced);
}
}
Result:
Logger.getInstance(RtpRuleEngineCompiledImpl.class)
The capture group ($1) replaces the entire string which was within the parentheses.
I know I can use "code" in GitHub Flavored Markdown to highlight a code snippet. But I am not able to display line numbers for a snippet. Is there a way to do so?
```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```
I want a line number to be put at the beginning of each line, like this:
1 var s = "JavaScript syntax highlighting";
2 alert(s);
As you may noticed in Markdown Cheatsheet, GitHub does not show line numbers in code blocks.
As a hack, you could save a pic of your code at https://carbon.now.sh and post it; they support line numbers as an option.
You can get something similar that you need using awk '{printf("% 4d %s\n", NR, $0)}' StartDsl.scala where StartDsl.scala is your source code file. Paste the result between
```scala
<your code here>
```
Although it is not available in GitHub as the question asks, I have discovered today if you add an = sign after the opening line, on some Markdown editors, it gives the desired result.
eg:
```javascript=
var s = "JavaScript syntax highlighting";
alert(s);
```
This works on Markdown editors such as HackMD
See your example on HackMD
So, you will need to help yourself by adding css to your html page. As a code goes into <pre> </pre> block in markdown.
You could apply your logic to this block to put line number against each line.
See https://codepen.io/heiswayi/pen/jyKYyg for reference.
I use RStudio with RMarkdown to render my Markdown (.md) files. It works great. Using the RMarkdown, the specification is made this way:
```{.javascript .numberLines .lineAnchors}
var s = "JavaScript syntax highlighting";
alert(s);
```
Yes, there are many markdown editors available and I'm not sure that this will work with all the editors, but RStudio/RMarkdown is a really great tool, which I use since a long time ago (IMHO).
Just add an = after the language you choose !
```java=
java code exemple:
int i = 5
```java=
Now here is the solution for adding line numbers in Markdown.
https://shd101wyy.github.io/markdown-preview-enhanced/#/markdown-basics?id=line-numbers
You can enable line number for a code block by adding line-numbers
class.
I am running in to bit of a trouble trying to use parametric replacement.
In my properties file I have the following entry
tpi.message=This is a test message. Generated for ? on ? .
The text above is displayed on the web and in the report therefore I need to replace ? with parameters.
However, I can't use replace* method because ? is special character for regex. I also don't want to use String.format method.
I know it is possible to replace ? but I don't remember how.
Your help is appreciated.
You can do it like this:
String message = "This is a test message. Generated for ? on ?";
message = message.replaceFirst("\\?", "Bob").replaceFirst("\\?", "Tuesday");
System.out.println(message); // This is a test message. Generated for Bob on Tuesday
I have the following Java code:
str = str.replaceAll("<.*?>.*?</.*?>|<.*?/>", "");
This turns a String like so:
How now <fizz>brown</fizz> cow.
Into:
How now cow.
However, I want it to just strip the <fizz> and </fizz> tags, or just standalone </fizz> tags, and leave the element's content alone. So, a regex that would turn the above into:
How now brown cow.
Or, using a more complex String, somethng that turns:
How <buzz>now <fizz>brown</fizz><yoda/></buzz> cow.
Into:
How now brown cow.
I tried this:
str = str.replaceAll("<.*?></.*?>|<.*?/>", "");
And that doesn't work at all. Any ideas? Thanks in advance!
"How now <fizz>brown</fizz> cow.".replaceAll("<[^>]+>", "")
You were almost there ;)
Try this:
str = str.replaceAll("<.*?>", "")
While there are other correct answers, none give any explanation.
The reason your regex <.*?>.*?</.*?>|<.*?/> doesn't work is because it will select any tags as well as everything inside them. You can see that in action on debuggex.
The reason your second attempt <.*?></.*?>|<.*?/> doesn't work is because it will select from the beginning of a tag up to the first close tag following a tag. That is kind of a mouthful, but you can understand better what's going on in this example.
The regex you need is much simpler: <.*?>. It simply selects every tag, ignoring if it's open/close. Visualization.
You can try this too:
str = str.replaceAll("<.*?>", "");
Please have a look at the below example for better understanding:
public class StringUtils {
public static void main(String[] args) {
System.out.println(StringUtils.replaceAll("How now <fizz>brown</fizz> cow."));
System.out.println(StringUtils.replaceAll("How <buzz>now <fizz>brown</fizz><yoda/></buzz> cow."));
}
public static String replaceAll(String strInput) {
return strInput.replaceAll("<.*?>", "");
}
}
Output:
How now brown cow.
How now brown cow.
This isn't elegant, but it is easy to follow. The below code removes the start and end XML tags if they are present in a line together
<url>"www.xml.com"<\url> , <body>"This is xml"<\body>
Regex :
to_replace='<\w*>|<\/\w*>',value=""
If you want to parse XML log file so you can do with regex {java}, <[^<]+<.so you get <name>DEV</name>. Output like name>DEV. You have to just play with REGEX.