2

I am interested in using the String.replaceAll function that will allow me to remove a specific sequence of a String.

  1. Does the functionality of String.replaceAll replace the first occurrence of the String sequence given to, well replace? My assumption is yes.

  2. Going about replacing the particular sub-string, how would I replace the characters in the provided String?

    String sentence = "@red@@blue@@green@Hello@reset@";
    

    The substring that I want to be removed is from the @ to the other @, in this case, while running String.replace

    String.replaceAll("@**PATTERN HERE SO NAME WON"T MATTER**@", "");
    

EDIT Seeing replace and replaceAll makes since between the two differences, being one using regex. My concern is removing the first occurrence of the expression given, being the original string of

String sentence = "@red@@blue@@green@Hello@reset@";

Would have to be ran 4 times in order to have "Hello" as the remaining member of the String.

Example: Run 1 - replaceFirstOccurence("@regex@", "");

System.out.println(sentence); --> "@blue@@green@Hello@reset@"

Run 2 - replaceFirstOccurence("@regex@", "");

System.out.println(sentence); --> "@green@Hello@reset@"

Run 3 - replaceFirstOccurence("@regex@", "");

System.out.println(sentence); --> "Hello@reset@"

Run 4 - replaceFirstOccurence("@regex@", "");

System.out.println(sentence); --> "Hello"
Kyle
  • 793
  • 6
  • 24
  • https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char,%20char) API docs are your friend. – jgr208 Feb 24 '16 at 17:20
  • `replace()` performs substitution of literals: single characters or strings. You need to use `replaceAll()`, which applies a regex, because the characters between two markers `@` are unknown. You can't use a literal substitution in that case. – nolexa Feb 24 '16 at 17:21
  • Wouldn't that in turn merely replace everything in occurrence to that regex? – Kyle Feb 24 '16 at 17:24
  • 2
    Please add an example. People are getting confused about what exactly you want. – Erick G. Hagstrom Feb 24 '16 at 17:27
  • this is getting worse and worse...you want to use a regex but want to use replace and not replaceall, will not work. – jgr208 Feb 24 '16 at 17:30
  • if you build the regex the correct way all occurrences will be removed. – jgr208 Feb 24 '16 at 17:33
  • 1
    @ErickG.Hagstrom I've added an example – Kyle Feb 24 '16 at 17:34
  • @jgr208 I don't want to nuke the string of everything on one go, basically I'm just easily removing the first occurrence then stopping. See the edit near the end as it shows the runs and what the String looks like after that particular run. – Kyle Feb 24 '16 at 17:35
  • Your example does not match your specs. According to what you say, the output of your example will be: "greenreset@". The eliminated substrings: @red@, @blue@, @Hello@. Please provide a consistent example. – nolexa Feb 24 '16 at 17:35
  • @KyleJ `(?<!foo.*)foo` This regex uses the negative lookbehind (?<!) to ensure no instance of foo is found prior to the one being matched. So use the look behind and you are golden, you can manipulate regex to as your heart desires. – jgr208 Feb 24 '16 at 17:36
  • 1
    @nolexa Look very carefully again at the String please, each color is governed by @color@. Having a color next to eachother would look like @color@@color@, therefore the Hello would indeed be last – Kyle Feb 24 '16 at 17:37
  • 1
    I believe he wants everything between @ and its following @ removed, so in his test string `@red@@blue@@green@Hello@reset@`, only `Hello` would remain. – Clark Kent Feb 24 '16 at 17:37
  • @jgr208 Could you use that in an example such as String.replaceAll()? Looking at it would be be like String.replaceAll("(?<!foo.*)foo", ""); then it stops – Kyle Feb 24 '16 at 17:38
  • @SaviourSelf Yes but only one at a time, not nuking the String on the first go. – Kyle Feb 24 '16 at 17:38
  • @KyleJ i literally found on this site and google, please do that on your own as well before coming here. here is the google search "regex negative look behind java replaceall" – jgr208 Feb 24 '16 at 17:38
  • @jgr208 I've seen that as well on google but it didn't seem to match what I was looking for entirely plus I did the research on this heavily before coming here but no one has played with this idea it seems. – Kyle Feb 24 '16 at 17:40
  • @KyleJ So why not return a string of the sequence up to the first index of `@`, and a substring of the second index of `@` to the end? Do you have to have a solution that uses `String.replaceAll`? – Clark Kent Feb 24 '16 at 17:42
  • @KyleJ http://denis-zhdanov.blogspot.com/2009/10/regexp-lookahead-and-lookbehind.html the reason you couldn't find it was because you didnt know what you were looking for. – jgr208 Feb 24 '16 at 17:42
  • Added a revision at the bottom to the answer. String.replaceFirst("@[^@]*@", ""); <-- Waxtahs regex – Kyle Feb 24 '16 at 17:44
  • 1
    @KyleJ Your example string is "@red@@blue@green@Hello@reset@", not "@red@@blue@@green@Hello@reset@". Do you see the difference? The markers around "green" are not balanced. Please pay attention. Your example is confusing people. – nolexa Feb 24 '16 at 17:45
  • Everybody needs to take a deep breath and let it out slowly. No sense in getting all prickly with each other. Relax, make sure you've reloaded since the last edit, and let's move forward, shall we? – Erick G. Hagstrom Feb 24 '16 at 18:03
  • Or just move away, since OP has his answer. – Erick G. Hagstrom Feb 24 '16 at 18:05

2 Answers2

6

use standart method of String replaceAll(String regularExpression, String replacement)

for example: "@some chars@".replaceAll("@[^@]*@", "@@");

waxtah
  • 306
  • 2
  • 10
  • but replace - does not support regular expression. – waxtah Feb 24 '16 at 17:22
  • I also need the pattern for @any length/character@ expression, not too well on this subject. – Kyle Feb 24 '16 at 17:23
  • 1
    I think he wants the @ symbols to be removed as well, so the second argument should be `""` – tyler Feb 24 '16 at 17:27
  • @waxtah I added an edit to the page, take a look at the bottom. Basically summarizing what I require since replaceAll would nuke the entire String of every occurrence of that rather than individually remove it. – Kyle Feb 24 '16 at 17:32
1

Alright to answer this question is actually extremely simple but the regex was the hard part.

Thanks to @waxtah for the regex that answered this question.

Using String.replaceFirst("@[^@]*@", "");

I was able to achieve my goal. Thanks to everyone here.

Kyle
  • 793
  • 6
  • 24