Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6381101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:21:11+00:00 2026-05-25T02:21:11+00:00

Here’s an example of the data: <animals><name>George</name><description>A big brown fox.</description></animals> It really doesn’t get

  • 0

Here’s an example of the data:

<animals><name>George</name><description>A big brown fox.</description></animals>

It really doesn’t get more complicated than that. I want to modify all text in the elements. (In this case, encrypt it).

What I’ve come up with so far is:

xml_data.gsub(/(<.*>)(.+)(<\/.*>)(?=<)/, "#{$1}#{$2.encrypt_string}#{$3}")

But, it only replaces the last element’s text. So I’m obviously missing something.

I invite any suggestions (including using REXML). I must use libraries standard with Ruby 1.8.7.

There is no chance of the XML being malformed because I wrote the process that produces it.

Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:21:12+00:00Added an answer on May 25, 2026 at 2:21 am

    Don’t use regular expressions for this, use a real parser such as Nokogiri:

    s = '<animals><name>George</name><description>A big brown fox.</description></animals>'
    d = Nokogiri::XML(s)
    d.search('//text()').each { |n| n.content = n.content.encrypt_string }
    s2 = d.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip
    

    Assuming of course that you have monkey patched encrypt_string into String somewhere.

    As far as your regex goes, (.+) is greedy and will happily consume </close_tag>, you have similar problems with .*.


    If you must use a regex (and it seems that you have choice), then you should tighten up your regex and switch to the block form of gsub to get sensible $1 and $2:

    xml_data.gsub(/<([^>]+)>([^<]+)<\/\1>/) { "<#{$1}>#{$2.encrypt_string}</#{$1}>" }
    

    Using [^>]+ and [^<]+ keeps you within the tags you want and the \1 back-reference is an easy to way match the opening and closing tags. For example, using upcase in place of encrypt_string does this:

    >> s = '<animals><name>George</name><description>A big brown fox.</description></animals>'
    >> s.gsub(/<([^>]+)>([^<]+)<\/\1>/) { "<#{$1}>#{$2.upcase}</#{$1}>" }
    => "<animals><name>GEORGE</name><description>A BIG BROWN FOX.</description></animals>"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is example: http://jsfiddle.net/pcNzb/9/ I just study, so i must have missed something or
Here is a pseudo code what I want. I need to get count of
here's my code. <input type=text placeholder=Add New Tag data-bind=value: tagToAdd, valueUpdate: 'afterkeydown', event: {
Here is an example: I write html code inside of textarea, then I swap
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is an example: I have the generic type called Account. I wish to
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is my error(if you need any more info just ask)- Error SQL query:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.