Hitachi Vantara Pentaho Community Forums
Results 1 to 9 of 9

Thread: Replacing a substring

  1. #1

    Default Replacing a substring

    Hi All,

    I am using Modified Java script value to replace a string value. My input is like: <id>refid_1234</id><value>1234</value>.

    I want to replace "<id>refid_1234</id>" with a Null. Want my final output as "<value>1234</value>".

    I tried replace(input,"<value>\\*</value>",""). But I could not acheive it, because it could not find "*" between id tags. (I can get these id tags anywhere in the string)

    How can I acheive this. Any help apreciated

    Thanks in advance
    Raghu

  2. #2
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    If your input is a well formed XML document, you can try this:

    Code:
    xf = new XML(input);
    delete xf.id;
    var output = xf;
    If only the two elements are there, this might work:

    Code:
    var output = "<" + input.split("><")[1];
    Or with a regular expression:

    Code:
    var output = input.replace(/<id>.*<\/id>/g, "");
    One more:

    Code:
    var output = input.substr(input.indexOf("<value>"));
    Last edited by marabu; 07-31-2012 at 08:58 AM.
    So long, and thanks for all the fish.

  3. #3

    Default

    Thanks marabu for your prompt reply.

    Unfortunately, my input is neither of the above. It is neither a well formatted XML nor there are only two elements in the string. The value i want to replace, can come anywhere in the string.

    And the "<value>" is expected more than once. Thanks in advance

    Thanks,
    Raghu
    Last edited by raghunadhn; 07-31-2012 at 09:13 AM.

  4. #4
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    This will find the two elements anywhere and delete the first:

    Code:
    var output = input.replace(/<id>.*<\/id>(<value>.*<\/value>)/g, "$1");
    So long, and thanks for all the fish.

  5. #5

    Default

    Thanks marabu for the reply. But it didn't work.

    Apologies for the confusion I might have caused. I have attached a file with my input in it.

    If you look it, the input is coming as a string, and I want to remove "<id>2200_refid</id>" from it. The value 2200_refid is not constant, it may vary. So without bothering about the content between "<id>" and "</id>", I want to get rid of "<id>'s" no matter how many times they appear in the string.

    Hope this helps you better in understanding my problem. Apologies once again and thanks for your effort

    Raghu
    Attached Files Attached Files

  6. #6
    Join Date
    Jun 2012
    Posts
    5,534

    Default

    There is an error in your sample data, I think.
    The first occurence of element <id> is outside of element group <Ad_Info>.
    Please, verify.
    So long, and thanks for all the fish.

  7. #7
    Join Date
    Nov 2008
    Posts
    271

    Default

    Quote Originally Posted by raghunadhn View Post
    Thanks marabu for the reply. But it didn't work.

    Apologies for the confusion I might have caused. I have attached a file with my input in it.

    If you look it, the input is coming as a string, and I want to remove "<id>2200_refid</id>" from it. The value 2200_refid is not constant, it may vary. So without bothering about the content between "<id>" and "</id>", I want to get rid of "<id>'s" no matter how many times they appear in the string.

    Hope this helps you better in understanding my problem. Apologies once again and thanks for your effort

    Raghu
    Code:
    var output = input.replace(/<id>.*?<\/id>/g, "");
    This expression removes all occurrences of <id>foo</id> pattern, whatever the string between the tags. Is it what you need?
    By the way, you get the same result from the "Replace in string" step, using RegEx.
    Last edited by Ato; 07-31-2012 at 02:19 PM. Reason: added the ref to Replace in string step
    Andrea Torre
    twitter: @andtorg

    join the community on ##pentaho - a freenode irc channel

  8. #8

    Default

    Thanks Andrea.. That worked like a miracle...

  9. #9

    Default

    Thanks for your reply marabu. I think the input is as expected. Thanks a lot for your efforts

    Raghu
    Last edited by raghunadhn; 08-01-2012 at 09:09 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.