Java remove dynamic substring from string - java

I need to remove dynamic substring from string. There is a few similar topic of this theme, but noone of them helped me. I have a string e.g.:
product test1="001" test2="abc" test3="123xzy"
and i need output:
product test1="001" test3="123xzy"
I mean I need remove test2="abc". test2 is an unique element and can be placed anywhere in original string. "abc" is dynamic variable and can have various length. What is the fastest and the most elegant solution of this problem? Thx

You can use a regular expression:
String input = "product test1=\"001\" test2=\"abc\" test3=\"123xzy\"";
String result = input.replaceAll("test2=\".*?\"\\s+", "");
In substance: find a substring like test2="xxxxxx", optionally followed by some spaces (\\s+) and replace it with nothing.

Related

Replace part of string with known beginning and end

I get some string from server with known and unknow parts. For example:
<simp>example1</simp><op>example2</op><val>example2</val>
I do not wish to parse XML or any use of parsing. What I wish to do is replace
<op>example2</op>
with empty string ("") which string will look like:
<simp>example1</simp><val>example2</val>
What I know it start with op (in <>) and ends with /op (in <>) but the content (example2) may vary.
Can you give me pointer how accomplish this?
You can use regex. Something like
<op>[A-Za-z0-9]*<\/op>
should match. But you can adapt it so that it fits your requirements better. For example if you know that only certain characters can be shown, you can change it.
Afterwards you can use the String#replaceAll method to remove all matching occurrences with the empty string.
Take a look here to test the regex: https://regex101.com/r/WhPIv4/3
and here to check the replaceAll method that takes the regex and the replacement as a parameter: https://developer.android.com/reference/java/lang/String#replaceall
You can try
str.replace(str.substring(str.indexOf("<op>"),str.indexOf("</op>")+5),"");
To remove all, use replaceAll()
str.replaceAll(str.substring(str.indexOf("<op>"),str.indexOf("</op>")+5),"");
I tried sample,
String str="<simp>example1</simp><op>example2</op><val>example2</val><simp>example1</simp><op>example2</op><val>example2</val><simp>example1</simp><op>example2</op><val>example2</val>";
Log.d("testit", str.replaceAll(str.substring(str.indexOf("<op>"), str.indexOf("</op>") + 5), ""));
And the log output was
D/testit: <simp>example1</simp><val>example2</val><simp>example1</simp><val>example2</val><simp>example1</simp><val>example2</val>
Edit
As #Elsafar said , str.replaceAll("<op>.*?</op>", "") will work.
Use like this:
String str = "<simp>example1</simp><op>example2</op><val>example2</val>";
String garbage = str.substring(str.indexOf("<op>"),str.indexOf("</op>")+5).trim();
String newString = str.replace(garbage,"");
I combined all the answers and eventually used:
st.replaceAll("<op>.*?<\\/op>","");
Thank you all for the help

Java regex: Replacing dynamic substrings

Suppose I have a String containing static tags that looks like this:
mystring = "[tag]some text[/tag] untagged text [tag]some more text[/tag]"
I want to remove everything between each tag pair. I've figured out how to do so by using the following regex:
mystring = mystring.replaceAll("(?<=\\[tag])(.*?)(?=\\[/tag])", "");
The result of which will be:
mystring = "[tag][/tag] untagged text [tag][/tag]"
However, I'm unsure how to accomplish the same goal if the opening tag is dynamic. Example:
mystring = "[tag parameter="123"]some text[/tag] untagged text [tag parameter="456"]some more text[/tag]"
The "value" of the parameter portion of the tag is dynamic. Somehow, I have to introduce a wildcard to my current regex, but I am unsure how to do this.
Essentially, replace the contents of all pairings of "[tag*]" and "[/tag]" with empty string.
An obvious solution would be to do something like this:
mystring = mystring.replaceAll("(?<=\\[tag)(.*?)(?=\\[/tag])", "");
However, I feel like that would be hacking around the problem because I'm not really capturing a full tag.
Could anyone provide me with a solution to this problem? Thanks!
I guess I've got it.
I thought long and hard about what #AshishMathew said, and yeah, lookbehinds can't have unfixed, lengths, but maybe instead of replacing it with nothing, we add a ] to it, like so:
mystring = mystring.replaceAll("(?<=\\[tag)(.*?)(?=\\[/tag])", "]");
(?<=\\[tag) is the look-behind which matches [tag
(.*?) is all the code between [tag and [/tag], which may even be the parameters of the tag, all of which is replaced by a ]
When I tried this code by replacing the match with "", I got [tag[/tag] untagged text [tag[/tag] as the output. Hence, by replacing the match with a ] instead of nothing, you get the (hopefully) desired output.
So this is my lazy solution (pardon the regex pun) to the problem.
I suggest matching the whole tag with content and replacing with the opening/closing tags without content :
mystring.replaceAll("\\[tag[^\\]]*\\][^\\[]*\\[/tag]", "[tag][/tag]")
Ideone test.
Note that I didn't bother conserving the tag attributes since you mentionned in another answer's comments that you didn't need them, but they could be kept by using a capturing group.

Most efficient way to get the substring after a specific other substring

If I have a string that looks something like this:
String text = "id=2009,name=Susie,city=Berlin,phone=0723178,birthday=1991-12-07";
I only want to have the info name and phone. I know how to parse the entire String, but in my specific case it is important to only get those two "fields".
So what is the best/most efficient way to have my search method do the following:
search for the substring "name=" and return the substring after it ("Susie") until it reaches the next comma
My approach would have been to:
get the last index of "name=" first
use this index then as the new start for my parsing method
Any other suggestions maybe on how this could be done more efficiently and with a more condense code? Thank you for any input
You can use following regex to capture the expected word after phone and name and get frist group from matched object:
(?:phone|name)=([^,]+)
With regards to following command if it might happen to have a word which is contain phone or name as a more comprehensive way you can putt a comma before your name.
(?:^|,)(?:phone|name)=([^,]+)
Read more about regular expression http://www.regular-expressions.info/
Regex might be more efficient, but for readability, I <3 Guava
String text = "id=2009,name=Susie,city=Berlin,phone=0723178,birthday=1991-12-07";
final Map<String, String> infoMap = Splitter.on(",")
.omitEmptyStrings()
.trimResults()
.withKeyValueSeparator("=")
.split(text);
System.out.println(infoMap.get("name"));
System.out.println(infoMap.get("birthday"));

Regex to extract value from link

I have a String like file:///android_asset/GwyXUyisyq. I want to extract the GwyXUyisyq from the rest of the string. The value will change in every instance, but the file:///android_asset/ will always remain fixed. What regex can I use to achieve the same?
You don't need a regex here :
Just find the last index of / and replace everything before it :)
String s = "file:///android_asset/GwyXUyisyq";
System.out.println(s.replace(s.substring(0,s.lastIndexOf("/")+1), ""));
O/P :GwyXUyisyq

How to get last few characters of varying length from a string ..?

I have a multiple string urls, from which i have to pick last few characters, which are id's infact. But the problem is that, the length of id's is not consistent, i.e., if one id is of length 6 then, other may be of length 5 or 4 and so on. The sample urls are like:
www.abc.com/xyz-123456
www.abc.com/pqr-5432
www.abc.com/lmn/opqr-25647
it could have been a lot easier if the length of the particular id portion would have been same, i could have used:
String abc = "www.abc.com/xyz-123456";
String id = abc.substring(abc.length()-6);
But now the scenario is different as length of id portion in the selected url is not the same always, How can i cater this varying id..???? please any help is appreciated.
There is a lastIndexOf method on the String object that will let you find the position of the '-' (I take it that is your separator). From there you can do the substring.
You can use something like this.
String id=abc.subString(abc.lastIndexOf('\'),abc.length()-1);
Hope it will help you. :)
String url1 = "www.abc.com/xyz-123456";
String[] url1Split = url1.split("-");
What you're looking for can be found in url1split[1]
Use regex to remove all characters upto -.
String id = url.replaceAll("^.*-","");
or
String id = url.replaceAll("^.*-(\\w+)$","$1");
You can use LastIndexOf or create the regular expression.

Categories