Java regular Expression End of String - java

I want to replace some part of a string, but only if the part is the last part of the string.
Example:
public class Test {
public static void banana(){
String test = "I love Banana";
System.out.println("Test: "+test+"\nRegEx: "+test.replace("a$","as"));
}
}
The code above does not change anything. Without the $-sign I get I love Basnasnas, which is not, what I want. I tried escape the dollar with a backslash, didn't work out. Where is my mistake?

Found the problem, I have to use replaceAll instead of replace! Thank you!

Related

Keep the same case when replacing words in Java

My goal is to correct common grammar errors in messages. Here's what I currently have written:
#EventHandler
public void onChat(AsyncPlayerChatEvent event){
String message = event.getMessage().replaceAll("(?i)dont", "don't")
.replaceAll("(?i)youre", "you're");
event.setMessage(message);
}
This works to replace dont with don't, and youre with you're. The issue is that DONT is replaced with don't, rather than DON'T.
How would I execute this replacement while preserving case?
Use capturing groups:
> "DoNt".replaceAll("(?i)\\b(don)(t)\\b", "$1'$2")
"DoN't" (String)
> "YoUrE".replaceAll("(?i)\\b(you)(re)\\b", "$1'$2")
"YoU'rE" (String)
You should also use \b for a word boundary, so you don't inadvertently change words like "orthodontist" into "orthodon'tist".

java regular expression partial replace

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.

Looking for Correct Java REGEX for this kind of payload

I have following two different payloads where I am trying to Write Java Regex:
Input Payload 1
ISA*00* *00* *ZZ*EXDO *ZZ*047336389 *150327*1007*U*00401*900063730*0*P*>~
GS*QM*EXDO*047336389*20150327*1007*900063730*X*004010~
ST*214*900063730~
B10*326GENT15173**EXDO~
L11*019*TN~
Input Payload 2
ISA*00* *00* *02*HJBT *01*047336389 *140103*1751*U*00401*000012003*0*P*>\
GS*QM*HJBT*047336389*20140103*1751*12003*X*004010\
ST*214*0001\
B10*117094*B065199*HJBT\
N1*SH*INTEVA PRODUCTS LLC-\
I have following REGEX:
.*(ST\*214|ST\*210).*
I tried to evaluate the REGEX on this URL http://www.regexplanet.com/advanced/java/index.html
I see matches() as NO for 1st Payload and matches() as YES for 2nd Payload. I am looking for Updated REGEX which actually works for BOTH conditions here.
My Purpose here to validate payload information just like String contains method can do it using following approach.
payload.toString().contains('ST*214') || payload.toString().contains('ST*210').
I want to use regex instead of string.contains here.
"(?s).*(ST\\*214|ST\\*210).*"
In Java you need to enable DOTALL mode (to make . match with line terminators too). This can be done by including (?s) modifier. You had match only in this ST*214*900063730~ particular part of first string.
use the following regexp:
".*(ST\*214|ST\*210).*"
Have tested your two strings with following code:
public class RegTest {
public static void main (String[] args) {
String test1 = "ISA*00* 00 ZZEXDO *ZZ*047336389*150414*1108*U*00401*979863647*0*P*>~ GSQMEXDO*047336389*20150414*1108*979863647*X*004010~ ST*214*979863647~ B10*186143**EXDO~";
String test2 = "ISA*00* 00 *02*HJBT *01*047336389*140103*1751*U*00401*000012003*0*P*>\\GSQMHJBT*047336389*20140103*1751*12003*X*004010\\ST*214*0001\\B10*117094*B065199*HJBT\\N1*SH*INTEVA PRODUCTS LLC-\\";
if (test1.matches(".*(ST\\*214|ST\\*210).*")) {
System.out.println("String1 matches");
}
if (test2.matches(".*(ST\\*214|ST\\*210).*")) {
System.out.println("String2 matches");
}
}
}
just small fix, regexp in comment lost two '\' characters. You can use the regexp from code.
I think you try to match the wildcard character '*' so you should use backslashes :
.*(ST\*214|ST\*210).*
or
.*ST\*(214|210).*
or
.*ST\*21(4|0).*
or
.*ST\*21[40].*
Are the linefeed part of your payload or just some formatting ?

how to remove unicode spaces from String returned by reader.readLine()

I when i use reader.readLine(), the string length is always 80 chars and after the main string unicode spaces are padded up.
Is there a way to remove those unwanted characters.
(java.io.RandomAccessFile reader)
String.trim is not working on this
You can use StringUtils.strip from Commons Lang. It is Unicode-aware.
You can write a custom method in Java to remove the Unicode space characters , using Character.isWhitespace(char) and Character.isSpaceChar(char) methods, for your specific purpose.
The Spring framework has a StringUtils class with a trimWhitespace(String) method which appears to be based on Character.isWhitespace(char) from the source code here.
use Google Guava
CharMatcher.WHITESPACE.trimFrom(source);
or try this https://gist.github.com/michalbcz/4861a2b8ed73bb73764e909b87664cb2
If you do not want a big libs. Just use:
str.replaceAll("^[\\s\\p{Z}]+|[\\s\\p{Z}]+$", "");
Testing
public static String trim(String str) {
return str.replaceAll("^[\\s\\p{Z}]+|[\\s\\p{Z}]+$", "");
}
public static void main(String[] args) {
System.out.println(trim("\t tes ting \u00a0").length());
System.out.println(trim("\t testing \u00a0").length());
System.out.println(trim("tes ting \u00a0").length());
System.out.println(trim("\t tes ting").length());
}
would have been faster to just search stackoverflow for this question becoz there are multiple questions on that topic there. well, try this:
st.replaceAll("\\s","")
check this one here: link

Java regex to strip out XML tags, but not tag contents

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.

Categories