Okay, so i'm working on a problem I'd rather solve with regex, I test most of my regex expressions in Notepad++, This has worked fine after a few tweaks such as double escaping some things for java, However this regex expression throws an exception when run in java, however it runs in Notepad++ just fine, the idea if this code is to be able to mention a different player in the game with a highlighted name.
tldr; I'm trying to replace the first occurrence of a specific name in a message
I've tried looking around for a while but i haven't found a solution, so i thought i might as well ask here.
p.getName() simply returns a string (the players name)
String newmessage = message.replaceFirst("(?i)" + Pattern.quote(p.getName()) + "((?(?=\\s)|('|,|!))|$)", color + p.getName() + Color.toString(Color.getLastColorOf(message)));
However executing the code throws this exception
...at java.lang.Thread.run(Unknown Source) [?:1.8.0_202]
Caused by: java.util.regex.PatternSyntaxException: Unknown inline modifier near index 15
(?i)\QTauCubed\E((?(?=\s)|('|,))|$)
^
at java.util.regex.Pattern.error(Unknown Source) ~[?:1.8.0_202]...
And I'm not sure what it wants me to do, I don't see how this is not valid regex
This is the regex for Notepad++
(?i)Name((?(?=\s)|('|,|!))|$)
The above will match
Name's r
Name
Name test
Name,
Name!
But will not match
Nametest
That is what I intended it to do.
I vote for just using the pattern \bName\b along with String#replaceFirst:
String input = "Rename here is a Name and here is the same Name again.";
input = input.replaceFirst("\\bName\\b", "blah");
System.out.println(input);
This prints:
Rename here is a blah and here is the same Name again.
Related
I'm trying to implement template with object, that can be plural.
For example: "There is/are n dog(s)."
When I tried There <#if dogNames?size>1>is<#else>are</#if> dogNames?size dog<#if dogNames?size>1>s</#if>, I received exception
freemarker.core.NonBooleanException: For "#if" condition: Expected a boolean, but this has evaluated to a number (wrapper: f.t.SimpleNumber):
==> dogNames?size
i.e. there is a problem with angle bracket used for comparison.
In this blog it is said that double quotes is enough to escape the bracket, but I'm not managed to do that in Java. When I declared it like this
String template = "There <#if dogNames?size\">\"1>is<#else>are</#if> dogNames?size dog<#if dogNames?size>1>s</#if>";
and called freemarker api, I received exception
freemarker.core.ParseException: Syntax error in template ...:
Encountered "\">\"", but was expecting one of:
"."
".."
<DOT_DOT_LESS>
"..*"
"?"
"??"
"!"
"["
"("
">"
<TERMINATING_EXCLAM>
I'm using freemarker 2.3.28, java 8
You can write <#if dogNames?size gt 1>. (See https://freemarker.apache.org/docs/dgui_template_exp.html#dgui_template_exp_comparison)
I want to get file name from complete path of file.
Input : "D://amol//1/\15_amol.jpeg"
Expected Output : 15_amol.jpeg
I have written below code for this
public class JavaApplication9 {
public static void main(String[] args) {
String fname="D://amol//1/\15_amol.jpeg";
System.out.println(fname.substring(fname.lastIndexOf("/")));
System.out.println(fname.substring(fname.lastIndexOf("\\")));
}
}
but getting below output :
_amol.jpeg
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -1
at java.lang.String.substring(String.java:1927)
at javaapplication9.JavaApplication9.main(JavaApplication9.java:6)
C:\Users\lakhan.kamble\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53:
Java returned: 1
The string \15 is an "octal escape" for the carriage return character (0x0d, 13 decimal). There are two possibilities here.
You really meant \15 to be the octal escape, in which case you are trying to create a filename with an embedded newline. The actual contents of fname in this case could be expressed as
"D://amol//1/" + "\n" + "_amol.jpeg";
Windows will prevent that from happening and your program will throw an IOException.
You really meant
String fname="D://amol//1/\\15_amol.jpeg";
In this case the extra backslash is redundant and will be ignored by Windows because the filename will resolve (in Windows path terms) to D:\amol\1\\15_amol.jpeg and adjacent directory separators collapse to a single separator. So you could just omit the extra backslash altogether without changing the effective path.
As to your exception, the string as shown DOES NOT contain a backslash character (case 1 above), so
fname.lastIndexOf("\\")
returned -1, causing the exception
The regular expression is
String regex = "^[\\p{IsHangul}\\p{IsDigit}]+";
And whenever i do
text.matches(regex);
It works fine in my system but not in some of the system.
I am not able to track the issue.
Thank you in advance.
Exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unknown character property name {Hangul} near index 13
^[\p{IsHangul}\p{IsDigit}]+
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.charPropertyNodeFor(Pattern.java:2437)
at java.util.regex.Pattern.family(Pattern.java:2412)
at java.util.regex.Pattern.range(Pattern.java:2335)
at java.util.regex.Pattern.clazz(Pattern.java:2268)
at java.util.regex.Pattern.sequence(Pattern.java:1818)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
at java.util.regex.Pattern.matches(Pattern.java:928)
at java.lang.String.matches(String.java:2090)
at com.mycompany.helper.ApplicationHelper.main(ApplicationHelper.java:200)
According to Using Regular Expressions in Java:
Java 5 fixes some bugs and adds support for Unicode blocks. ...
Make sure you're using Java 5+ in the server.
It seems that Java version you are using is not able to recognise Hangul as correct script character so you can try to create your own character class which will cover same range as Hongul from newer versions of Java.
From what I see in code in source code of Character.UnicodeScript on Java 8 Hangul refers to Unicode ranges
1100..11FF
302E..302F
3131..318F
3200..321F
3260..327E
A960..A97F
AC00..D7FB
FFA0..FFDF
so maybe try with such pattern
Pattern.compile("^["
+ "\u1100-\u11FF"
+ "\u302E-\u302F"
+ "\u3131-\u318F"
+ "\u3200-\u321F"
+ "\u3260-\u327E"
+ "\uA960-\uA97F"
+ "\uAC00-\uD7FB"
+ "\uFFA0-\uFFDF"
+ "\\p{IsDigit}]+");
I am developing an application in which I need to process text files containing emails. I need all the tokens from the text and the following is the definition of token:
Alphanumeric
Case-sensitive (case to be preserved)
'!' and '$' are to be considered as constituent characters. Ex: FREE!!, $50 are tokens
'.' (dot) and ',' comma are to be considered as constituent characters if they occur between numbers. For ex:
192.168.1.1, $24,500
are tokens.
and so on..
Please suggest me some open-source tokenizers for Java which are easy to customize to suit my needs. Will simply using StringTokenizer and regex be enough? I have to perform stopping also and that's why I was looking for an open source tokenizer which will also perform some extra things like stopping, stemming.
A few comments up front:
From StringTokenizer javadoc:
StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended
that anyone seeking this functionality use the split method of String
or the java.util.regex package instead.
Always use Google first - the first result as of now is JTopas. I did not use it, but it looks it could work for this
As for regex, it really depends on your requirements. Given the above, this might work:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Mkt {
public static void main(String[] args) {
Pattern p = Pattern.compile("([$\\d.,]+)|([\\w\\d!$]+)");
String str = "--- FREE!! $50 192.168.1.1 $24,500";
System.out.println("input: " + str);
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println("token: " + m.group());
}
}
}
Here's a sample run:
$ javac Mkt.java && java Mkt
input: --- FREE!! $50 192.168.1.1 $24,500
token: FREE!!
token: $50
token: 192.168.1.1
token: $24,500
Now, you might need to tweak the regex, for example:
You gave $24,500 as an example. Should this work for $24,500abc or $24,500EUR?
You mentioned 192.168.1.1 should be included. Should it also include 192,168.1,1 (given . and , are to be included)?
and I guess there are other things to consider.
Hope this helps to get you started.
I am receiving this error when trying to execute applescript from my java application. The code is as follows:
String script = "tell application \"Terminal\" to do shell script \"/System/Library/CoreServices/Menu\\ Extras/user.menu/Contents/Resources/CGSession -suspend\" ";
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("AppleScript");
engine.eval(script);
I get the following error:
Exception in thread "main" javax.script.ScriptException: Expected “"” but found unknown token.
at apple.applescript.AppleScriptEngine.evalScript(Native Method)
at apple.applescript.AppleScriptEngine.eval(AppleScriptEngine.java:342)
at apple.applescript.AppleScriptEngine.eval(AppleScriptEngine.java:313)
at myTestApp.Main.main(Main.java:25)
Thanks for your consideration.
A guess based on experience... Maybe the escaped space in the pathname is your show stopper.
Try to call a script from a location where the path has no spaces or try to 'double-escape' the escaped space, like so:
"tell application \"Terminal\" to do shell script \"/System/Library/CoreServices/Menu\\\\ Extras/user.menu/Contents/Resources/CGSession -suspend\" "
A common reason for strange errors are those whitespaces in pathnames. So it was my first guess, that this causes trouble in your script. Then I remembered that sometimes we have to 'escape escaped backslashes'. This article doesn't explain, why it solved exactly your problem, but it shows how many backslashes may be needed...
You need to 'double-escape' the space in the path:
vvvv
...\Menu\\\\ Extras\...