regex  text to xml

Posted on 2012-09-04
Last Modified: 2012-09-04
Text: "Business & Industrial > Heavy Machinery > Chippers"
XML: Business & Industrial > Heavy Machinery > Chippers

"Business & Industrial > Heavy Machinery > Chippers"
want this as output
Business & Industrial > Heavy Machinery > Chippers
Question by:rgb192
    LVL 10

    Assisted Solution

    In Perl this would be:
    $text =~ s/</&lt;/g;
    $text =~ s/>/&gt;/g;
    $text =~ s/&/&amp;/g;

    Note that you must replace < and > before &.
    LVL 84

    Assisted Solution

    I'd do that in the opposite order,
     s/&/&amp;/g, s/</&lt;/g, s/>/&gt;/g for $text;
    or replace them all at once as
    $text =~ s/([<>&])/&${{'<'=>'lt','>'=>'gt','&'=>'amp'}}{$1};/g;
    LVL 10

    Expert Comment

    @ozo, you're right about the order.
    LVL 74

    Accepted Solution

    Why even use a regex? A simple string replace should suffice. Which language are you using?

    Author Comment

    I am using php

    Author Closing Comment

    thank you
    I am using php and have an updated question of how to do this in php

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now