11 Replies Latest reply: Aug 21, 2008 10:48 AM by 800282 RSS

    Converting sed regex to Java regex

    807589
      I am new to reguler expressions.I have to write a regex which will do some replacements

      If the input string is something like test:17038199556@test.com;value=abcd
      when I apply the regex on it,this string should be changed to test:17038199556@replaceTest.com;value=replacedABC

      I am trying to replace test.com and abcd with the values i have supplied...

      The regex I have come up with is in sed

      s/\(^.*@\)\(.*\)$/\1replaceTest.com;value=replacedABC/i

      Now I am trying to get the regex in Java,I would think it will be something like (^.*@\)(.*\)$\1replaceTest.com;value=replacedABC

      But not sure How i can test this.Any idea on how to make sure my java regex is valid and does the required replacements?
        • 1. Re: Converting sed regex to Java regex
          800282
          rsv-us wrote:
          I am new to reguler expressions.I have to write a regex which will do some replacements

          If the input string is something like test:17038199556@test.com;value=abcd
          when I apply the regex on it,this string should be changed to test:17038199556@replaceTest.com;value=replacedABC

          I am trying to replace test.com and abcd with the values i have supplied...

          The regex I have come up with is in sed

          s/\(^.*@\)\(.*\)$/\1replaceTest.com;value=replacedABC/i

          Now I am trying to get the regex in Java,I would think it will be something like (^.*@\)(.*\)$\1replaceTest.com;value=replacedABC

          But not sure How i can test this.Any idea on how to make sure my java regex is valid and does the required replacements?
          So, you want to replace everything that is between the '@' and the '.' and you want to replace that what comes after the '=' ? If not, then please provide more details.
          Also, have a look at String's replaceAll(...) method: the first parameter is a regex.
          • 2. Re: Converting sed regex to Java regex
            807589
            Nope,I want to replace the value after @ and till the ;

            and replace everything after = if this value is present like in cases like,if the input string is just only

            test:17038199556@test.com,then the output should be test:17038199556@replaceTest.com

            If the input string contains value also,then it will have that after ; as test:17038199556@test.com;value=abcd,then the output should be test:17038199556@replaceTest.com;value=replacedABC

            To put it in other words,from the input String,replace the string from @ till ; and strip out the value=abcd,if present and then append the one what I give in the regex,if any.

            I want this whole operation to be done in the regex...Didn;t find String's replaceAll(...) method: much useful.

            Thanks
            • 3. Re: Converting sed regex to Java regex
              800282
              rsv-us wrote:
              ...
              I want this whole operation to be done in the regex...
              Why not call it twice?
              Didn;t find String's replaceAll(...) method: much useful.

              Thanks
              Too bad, because that is the only way to replace text using regex with Java. And you will have to call the method twice.
              • 4. Re: Converting sed regex to Java regex
                807589
                ok,since that this has to be done thru regex only..and the regex I gave works in sed with adding sed required /

                This is the regex we send to the thrid party API,probably its upto them to use either sed to do this replacement or java i guess.I guess I want to know more on how we can test these substitution regex?

                Thanks
                • 5. Re: Converting sed regex to Java regex
                  800282
                  rsv-us wrote:
                  ... I guess I want to know more on how we can test these substitution regex?
                  I don't understand this question.

                  Just to be clear: Java's replaceAll(...) method cannot do multiple replacements like sed can. Period. You will have to call the method twice:
                  String s = "test:17038199556@test.com;value=abcd";
                  s = s.replaceAll("your regex", "your replacement");
                  s = s.replaceAll("your regex", "your replacement");
                  System.out.println(s);
                  • 6. Re: Converting sed regex to Java regex
                    800282
                    rsv-us wrote:
                    ...
                    The regex I have come up with is in sed

                    s/\(^.*@\)\(.*\)$/\1replaceTest.com;value=replacedABC/i

                    ...
                    BTW, I'm no sed expert, but I can't imagine that really works. AFAIK, multiple replacements in sed are done like this:
                    sed 's/text1/replacement1/; s/text2/replacement2/'
                    • 7. Re: Converting sed regex to Java regex
                      807589
                      Yep.Agreed.

                      Since that these replacements should be done in a single regex.I think once we send this regex to the third party,they will haev to use either sed or perl(will perl do this replacements,not sure though) to get the output.

                      Since we are not sure what tool/software the third party is going to use,I was trying to see how i can really test this.Then I read about sed and this regex as is didn't work,so,I had to put all the sed required / and then the regex had become like s/\(^.*@\)\(.*\)$"/1replaceTest.com;value=replacedabcd/i

                      since that we will have to send the java regex to the third party,I was trying to see how i can convert this sed regex to java.If I am right,with jave regex,we won;t be able to all the finds and replacements in a single regex..right?...If this is true,this will leave me a question of whether I need to send the sed regex to the thrid party or If I send java regex,they have to convert that to either sed or perl regex.

                      One more question,can we do thse replacement in perrl also,if so,what will the equivalent regex for this in perl?
                      • 8. Re: Converting sed regex to Java regex
                        800282
                        rsv-us wrote:
                        Yep.Agreed.

                        Since that these replacements should be done in a single regex.
                        Note that the sed replacement I posted is really made of two replacements! Just like your Java solution would.
                        I think once we send this regex to the third party,they will haev to use either sed or perl(will perl do this replacements,not sure though) to get the output.

                        Since we are not sure what tool/software the third party is going to use,I was trying to see how i can really test this.Then I read about sed and this regex as is didn't work,so,I had to put all the sed required / and then the regex had become like s/\(^.*@\)\(.*\)$"/1replaceTest.com;value=replacedabcd/i
                        Again: AFAIK that does not work. I tried it like this:
                        {code}$ echo test:17038199556@test.com;value=abcd | sed 's/\(^.*@\)\(.*\)$"/1replaceTest.com;value=replacedabcd/i'
                        and the following is returned:
                        test:17038199556@test.com
                        since that we will have to send the java regex to the third party,I was trying to see how i can convert this sed regex to java.If I am right,with jave regex,we won;t be able to all the finds and replacements in a single regex..right?...If this is true,this will leave me a question of whether I need to send the sed regex to the thrid party or If I send java regex,they have to convert that to either sed or perl regex.

                        One more question,can we do thse replacement in perrl also,if so,what will the equivalent regex for this in perl?
                        I can't understand what you are talking about. The large amount of spelling errors also doesn't help to make it clearer. Good luck though.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                        • 9. Re: Converting sed regex to Java regex
                          807589
                          Ok,I guess the regex u have used is different than What i have got,try this

                          sed s/\(^.*@\)\(.*\)$/\1replaceTest.com;value=replacedABC/i

                          this will work.

                          Also sorry about the spelling mistakes,didn't realize them while typing...The question I was asking at the end is,does Perl also does this kind of replacements,if so...do u have any idea of what might be the equivalent regex in perl for this?

                          Thanks
                          • 10. Re: Converting sed regex to Java regex
                            800282
                            rsv-us wrote:
                            Ok,I guess the regex u have used is different than What i have got,try this

                            sed s/\(^.*@\)\(.*\)$/\1replaceTest.com;value=replacedABC/i

                            this will work.
                            Executing:
                            echo test:17038199556@test.com;value=abcd | sed 's/\(^.*@\)\(.*\)$/\1replaceTest.com;value=replacedABC/i'
                            in my Linux machine, results in the following output:
                            test:17038199556@test.com
                            But no matter, if it works with you, all is fine.
                            Also sorry about the spelling mistakes,didn't realize them while typing...
                            No harm done.
                            The question I was asking at the end is,does Perl also does this kind of replacements,if so...do u have any idea of what might be the equivalent regex in perl for this?

                            Thanks
                            No idea. I'd try asking in a Perl forum.

                            Best of luck.
                            • 11. Re: Converting sed regex to Java regex
                              807589
                              You can do the kind of thing you're talking about with Perl's /e modifier or Ruby's gsub command, both of which allow you to embed program logic in the replacement text. However, there would be no way to translate those regexes to any other language. Another possibility is to include Elliott Hughes' Rewriter in your app. Here's how you might use it:
                              String str = "test:17038199556@test.com;value=abcd";
                              
                              String result = new Rewriter("^([^@]++@)([^;]++)(?:(;value=)(.+))?$")
                              {
                                public String replacement()
                                {
                                  StringBuilder sb = new StringBuilder();
                                  sb.append(group(1)).append("REPLACE").append(group(2));
                                  if (group(3) != null)
                                  {
                                    sb.append(group(3)).append("REPLACE").append(group(4));
                                  }
                                  return sb.toString();
                                }
                              }.rewrite(str);
                              
                              System.out.println(result);
                              Not exactly a one-liner, I know, but it will let your users test their regexes in the language the app is written in.