Java Pattern Matcher single or multiple with comma separated - java

I have a string, which I need to parse, I want to use pattern matcher
need help with pattern.
if string as below:
sometext : test1,test2
output should be:
test1
test2
if input string is :
sometext : test1
then output should be :
test1
as you can see, it can be multiple or single.

So, you just need to replace , with a space? I would suggest a simple
String output = sometext.replace(",", " ");
If you need a newline after the first word, you can do
String output = sometext.replace(",", System.getProperty("line.separator"));
instead.
If "sometext : " is included in the input, you can get rid of that first in the same way:
String output = input.replace("sometext : ", "").replace(",", " ");

First, you have to separate "test1,test2" from "sometext", then use replaceAll to get the tests array by the , token.
String foo = "sometext : test1,test2";
String[] fooArr = foo.split("[:]");
String tests = fooArr[1];
System.out.println(tests.replaceAll(",", " "));
​

Related

Regex to find text between string pattren

String: [img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]
Result I Want: [img border=0]images/bbcode/sets/misc/bullet_go.png[/img] without /scm/ text.
Issue: Text scm is not static, could be any other text in data.
What I want: Have a look to this string
[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]
Regex which can fetch a text between ] and images/bbcode/ so the regex will detect the \scm\ text and then can remove this \scm\ from String data and end result will look like
[img border=0]images/bbcode/sets/misc/bullet_go.png[/img]
PS: I am implementing this logic in Java.
you can reach the goal without using regex, too.
since you said that the other parts are static, try this:
String myStr = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
myStr = "[img border=0]" + myStr.substring(myStr.indexOf("images"));
System.out.println(myStr);
and the output will be:
[img border=0]images/bbcode/sets/misc/bullet_go.png[/img]
I have captured text between '] and /images..' and replace this text with "". Check the following demo:
String s = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
s = s.replaceAll("(?<=])/[^/]+/","");
System.out.println(s);
if [img border=0] dynamic, you can take all except /scm/
some demo
String input = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
Pattern p = Pattern.compile("(^.*\\])\\/.*?\\/(.*$)");
Matcher m = p.matcher(input);
if (m.find()) {
String output = m.replaceFirst("$1$2");
System.out.println(output);
}
// -> [img border=0]images/bbcode/sets/misc/bullet_go.png[/img]
I found one more way to solve this same problem
String pattereString = "].*/images";
String maineString = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
maineString = maineString.replaceAll(pattereString, "images");
System.out.println(maineString);
Output:
[img border=0]images/bbcode/sets/misc/bullet_go.png[/img]

regular expression to replaceall substrings embedded in open curling brackets and followed by equal sign and digits

In the follwing String
String toBeFormatted= "[[LngLatAlt{longitude=-7.125924901999952, latitude=33.831783175000055, altitude=NaN},
LngLatAlt{longitude=-5.401396163999948, latitude=35.92213140900003, altitude=NaN}]]"
1- I need to replace all "LngLatAlt{longitude=" with open bracket "["
2- also need to replace all the intermediate ", latitude=33.831783175000055, altitude=NaN}" with ",33.831783175000055]"
That way my string result :
"[[[-7.125924901999952,33.831783175000055],[-5.401396163999948,35.92213140900003]]]"
try it the following reg exp :
String regexTarget = "(\\[\\[LngLatAlt\\{longitude=)";
toBeFormatted.replaceAll(regexTarget, "\\[\\[\\[");
String regexTarget0 = "(, altitude=NaN\\}, LngLatAlt\\{longitude=)";
toBeFormatted.replaceAll(regexTarget0, "],\\[");
String regexTarget1 = "(, latitude=)";
toBeFormatted.replaceAll(regexTarget1, " ,");
String regexTarget2 = "(, altitude=NaN\\})";
toBeFormatted.replaceAll(regexTarget2, "]");
but it seems not working.
Thank you for your help.
try something like:
String result = toBeFormatted.replaceAll("LngLatAlt\\{longitude=([^,]+), latitude=([^,]+), ([^}]+)\\}", "[$1, $2]");
System.out.println(result);

How to remove commas at the end of any string

I have Strings "a,b,c,d,,,,, ", ",,,,a,,,,"
I want these strings to be converted into "a,b,c,d" and ",,,,a" respectively.
I am writing a regular expression for this. My java code looks like this
public class TestRegx{
public static void main(String[] arg){
String text = ",,,a,,,";
System.out.println("Before " +text);
text = text.replaceAll("[^a-zA-Z0-9]","");
System.out.println("After " +text);
}}
But this is removing all the commas here.
How can write this to achieve as given above?
Use :
text.replaceAll(",*$", "")
As mentioned by #Jonny in comments, can also use:-
text.replaceAll(",+$", "")
Your first example had a space at the end, so it needs to match [, ]. When using the same regular expression multiple times, it's better to compile it up front, and it only needs to replace once, and only if at least one character will be removed (+).
Simple version:
text = text.replaceFirst("[, ]+$", "");
Full code to test both inputs:
String[] texts = { "a,b,c,d,,,,, ", ",,,,a,,,," };
Pattern p = Pattern.compile("[, ]+$");
for (String text : texts) {
String text2 = p.matcher(text).replaceFirst("");
System.out.println("Before \"" + text + "\"");
System.out.println("After \"" + text2 + "\"");
}
Output
Before "a,b,c,d,,,,, "
After "a,b,c,d"
Before ",,,,a,,,,"
After ",,,,a"

I want to check if a word or a set of words exists in a String

My requirement is to check if a group of words or a single word is present in a larger string. I tried using String.contains() method but this fails in case the larger string has new line character. Currently I am using a regex mentioned below. But this works for only one word. The searched text is a user entered value and can contain more than one word. This is an android application.
String regex = ".*.{0}" + searchText + ".{0}.*";
Pattern pattern = Pattern.compile(regex);
pattern.matcher(largerString).find();
Sample String
String largerString ="John writes about this, and John writes about that," +
" and John writes about everything. ";
String searchText = "about this";
Why not just replace line breaks with spaces, and on top of that, convert it all to lower case?
String s = "hello";
String originalString = "Does this contain \n Hello?";
String formattedString = originalString.toLowerCase().replace("\n", " ");
System.out.println(formattedString.contains(s));
Edit: Thinking about it, I don't really understand how line breaks make a difference...
Edit 2: I was right. Line breaks don't matter.
String s = "hello";
String originalString = "Does this contain \nHello?";
String formattedString = originalString.toLowerCase();
System.out.println(formattedString.contains(s));
here is code not using regex.
String largerString = "John writes about this, and John writes about that," +" and John writes about everything. ";
String searchText = "about this";
Pattern pattern = Pattern.compile(searchText);
Matcher m = pattern.matcher(largerString);
if(m.find()){
System.out.println(m.group().toString());
}
Result:
about this
I hope it will help you.

removing space before new line in java

i have a space before a new line in a string and cant remove it (in java).
I have tried the following but nothing works:
strToFix = strToFix.trim();
strToFix = strToFix.replace(" \n", "");
strToFix = strToFix.replaceAll("\\s\\n", "");
myString.replaceAll("[ \t]+(\r\n?|\n)", "$1");
replaceAll takes a regular expression as an argument. The [ \t] matches one or more spaces or tabs. The (\r\n?|\n) matches a newline and puts the result in $1.
try this:
strToFix = strToFix.replaceAll(" \\n", "\n");
'\' is a special character in regex, you need to escape it use '\'.
I believe with this one you should try this instead:
strToFix = strToFix.replace(" \\n", "\n");
Edit:
I forgot the escape in my original answer. James.Xu in his answer reminded me.
Are you sure?
String s1 = "hi ";
System.out.println("|" + s1.trim() + "|");
String s2 = "hi \n";
System.out.println("|" + s2.trim() + "|");
prints
|hi|
|hi|
are you sure it is a space what you're trying to remove? You should print string bytes and see if the first byte's value is actually a 32 (decimal) or 20 (hexadecimal).
trim() seems to do what your asking on my system. Here's the code I used, maybe you want to try it on your system:
public class so5488527 {
public static void main(String [] args)
{
String testString1 = "abc \n";
String testString2 = "def \n";
String testString3 = "ghi \n";
String testString4 = "jkl \n";
testString3 = testString3.trim();
System.out.println(testString1);
System.out.println(testString2.trim());
System.out.println(testString3);
System.out.println(testString4.trim());
}
}

Categories