-5

I have few strings

  1. "abcd: 1234"
  2. "abcd : 1234"
  3. "abcd : 1234"
  4. "abcd: 1234"
  5. "abcd 1234"
  6. "abcd : abcd dgdfgdf abcd dgdsfsdf"

Please help with java code to replace abcd with xyz without removing the spaces in the first 4 strings and leave the 5th one as is and the 6th one only the first instance of abcd is replaced with 1234 as the pattern to match is abcd :

Pshemo
  • 122,468
  • 25
  • 185
  • 269
micheal marquiz
  • 321
  • 2
  • 5
  • 18

3 Answers3

0

Try this:

public class Test {     
    public static void main(String[] args) {
        String[] saveSpace = {
            "abcd: 1234",
            "abcd : 1234",
            "abcd : abcd dgdfgdf abcd dgdsfsdf",
            "abcd 1234",
            "asdasdas abcd abcd: sdfdsf"
        };

        String regex = "abcd(?!\\s*\\w)(?=(?:[^:]*\\:){1}[^:]*$)";
        String replace = "xyz";

        for(int i = 0; i<saveSpace.length; i++) {
            saveSpace[i] = saveSpace[i].replaceFirst(regex, replace);
            System.out.println(saveSpace[i]);
        }
    }
}

This outputs:

xyz: 1234
xyz : 1234
xyz : abcd dgdfgdf abcd dgdsfsdf
abcd 1234
asdasdas abcd xyz: sdfdsf

It should matches abcd followed by anynumber of whitespace, a ; and no characters from [a-zA-Z0-9]

Dan
  • 7,286
  • 6
  • 49
  • 114
  • Yes. That method should still work unless I am missing something – Dan Jan 18 '16 at 20:27
  • it wont work if the string is "asdasdas abcd abcd: sdfdsf" .. In this case i should replace only the second instance of abcd and remember that the spaces should be ignored between abcd and : – micheal marquiz Jan 18 '16 at 20:30
  • I see what you are getting at now – Dan Jan 18 '16 at 20:31
  • @michealmarquiz Let me know if it doesn't – Dan Jan 18 '16 at 21:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101013/discussion-between-micheal-marquiz-and-dan). – micheal marquiz Jan 18 '16 at 21:16
  • 1
    @Dan Don't assign parameters (as seen in your `checkString()` method). That's generally a [bad practice](http://stackoverflow.com/a/2971004/5717099). – morido Jan 18 '16 at 21:20
  • @morido I corrected my mistake. Thank you for pointing that out – Dan Jan 18 '16 at 21:28
0

So here is the requested quick answer since everyone else seems to prefer discussions over code...

String input[] = {
    "abcd: 1234",
    "abcd : 1234",
    "abcd    : 1234",
    "abcd:       1234",
    "abcd 1234",
    "abcd : abcd dgdfgdf abcd dgdsfsdf",
    "abcd abcd: dgdfgdf abcd dgdsfsdf",
    "sdsdsdsad abcd abcd : dgdfgdf abcd dgdsfsdf"
};

for (String s: input) {
    String output = s.replaceFirst("^(.*)abcd((?=\\s*:).*)$", "$1xyz$2");
    System.out.println(output);
}   

This gives you:

xyz: 1234
xyz : 1234
xyz    : 1234
xyz:       1234
abcd 1234
xyz : abcd dgdfgdf abcd dgdsfsdf
abcd xyz: dgdfgdf abcd dgdsfsdf
sdsdsdsad abcd xyz : dgdfgdf abcd dgdsfsdf

Hope this helps (and ends the discussions).

morido
  • 1,027
  • 7
  • 24
  • Thanks a lot.. but once small issue.. abcd can be anywhere not just the first instance.. Example: "abcd abcd: dgdfgdf abcd dgdsfsdf", "sdsdsdsad abcd abcd : dgdfgdf abcd dgdsfsdf" only "abcd: " or "abcd : " should be replaced in these examples – micheal marquiz Jan 18 '16 at 21:02
  • @michealmarquiz I've edited the answer accordingly. Am I guessing right that `abcd :` occurs at most once per line? – morido Jan 18 '16 at 21:09
-1

Try this:

public void checkString(String s) {
    if(s.contains(":"))
        s.replaceFirst("abcd", "xyz");
}

Then call the checkString and insert each one of your strings.

checkString(string1)
checkString(string2)
...

The replaceFirst function in the String class replace the first occurence of the first string with the second string. The contains function check if the string contains the string ":" (in this case) and return true if it exists.

sguan
  • 1,039
  • 10
  • 26
  • 1
    `your_string` can't be 4 strings. Just writing a line of code is not good advice. – PA. Jan 18 '16 at 20:12
  • Sorry, I did not post the complete question.. Please see the update.. I dont really want to replace all the strings match.. I want to replace a pattern abcd :, there can be any number of spaces – micheal marquiz Jan 18 '16 at 20:14
  • it is no one string, just few examples of strings – micheal marquiz Jan 18 '16 at 20:15
  • string can have multiple abcd for example "abcd : abcd dgdfgdf abcd dgdsfsdf" .. in this case only the first instance should be replaced – micheal marquiz Jan 18 '16 at 20:21
  • @Pshemo OP's question was edited over 9000 times. How can I keep up with that? – sguan Jan 18 '16 at 20:27
  • 2
    But Java rules don't change that often and your code has problem which would be obvious if you would test it. So please before you post/update your answer make sure it is correct (this is not a race). – Pshemo Jan 18 '16 at 20:28
  • 2
    @michealmarquiz "*so many bullies here*" what makes you feel that way? Purpose of Stack Overflow is being searchable repository of clear programming questions and answers. If either question/answer has low quality (is unclear or simply wrong) then it is not useful for others which means in current form it doesn't belong here so we are asking its authors for corrections/clarifications. – Pshemo Jan 18 '16 at 20:42
  • 1
    Well if a question is of low quality, at-least give an opportunity edit the question, check the answers by people over here. Instead of assuming that OP did not try, how about trying to actullay help? bullying people who are trying to help, bay saying that code was not tested etc.. closing the question without giving an opportunity to rectify a mistake. If people spend enough time checking APIs, books, trying out code etc.. – micheal marquiz Jan 18 '16 at 20:52
  • 1
    I am pretty sure a solution could be found to most of the issues without posting here. Sometimes people might be in a hurry to get quick answer or try in a correct direction and they post a question here. An issue might be simple to you but please dont assume its simple to everyone. – micheal marquiz Jan 18 '16 at 20:52
  • 1
    @michealmarquiz "*at-least give an opportunity edit the question*" I am not stopping you, I even edited your question few times to improve its clarity by formatting your examples to show spaces properly. "*Instead of assuming that OP did not try*" I am not assuming anything here, but seeing problem you are facing while writing code could help us help you better. Also it could be useful for future reader to help hiem decide if this question/problem is what he is looking for. So it is always good to show [what have you tried](http://whathaveyoutried.com) and describe how it didn't work for you. – Pshemo Jan 18 '16 at 21:23
  • 1
    @michealmarquiz "*bullying... bay saying that code was not tested*" I am trying to give author of this answer hint that there is some very basic problem in his code which needs to be corrected. Also I didn't down-vote this answer nor your question. "*closing the question without giving an opportunity to rectify a mistake*" you ware asked to clarify your question in comments before I cast my close vote (and I did because your question was still unclear). "*Sometimes people might be in a hurry to get quick answer*" is irrelevant here, we expect some minimal quality in questions. – Pshemo Jan 18 '16 at 21:23