Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

Need a regular expresion

Hi regex gurus,

I need a regex that converts the ISO 8601 format duration string into a user readable string.
The format is: PnYnMnDTnHnMnS where
P  = period delimiter
Y = years
M = Months
D = Days
T = Time delimiter
H = hours
M = minutes
S = Seconds

So for the input string "P2DT23H32M51S" the result should be "2 days 23 hours 32 minutes 51 seconds"

I'm using .net 1.1 c#

Thanks,
Alex
0
astankovic
Asked:
astankovic
  • 3
1 Solution
 
ZvonkoSystems architectCommented:
Check this:


<script>

var thePeriod = "P1Y0M2DT23H32M51S";

var perText = thePeriod.replace(/[PT]/g,"");
perText = perText.replace("Y"," years ");
perText = perText.replace("M"," months ");
perText = perText.replace("D"," days ");
perText = perText.replace("H"," hours ");
perText = perText.replace("M"," minutes ");
perText = perText.replace("S"," seconds");
 

</script>

0
 
ZvonkoSystems architectCommented:
Or better this:

<script>

var thePeriod = "P2DT23H32M51S";

var perGroup = thePeriod.match(/P(\d+Y)?(\d+M)?(\d+D)?T(\d+H)?(\d+M)?(\d+S)?/);
perText = "";
if(perGroup[1]) perText += perGroup[1].replace("Y"," years ");
if(perGroup[2]) perText += perGroup[2].replace("M"," months ");
if(perGroup[3]) perText += perGroup[3].replace("D"," days ");
if(perGroup[4]) perText += perGroup[4].replace("H"," hors ");
if(perGroup[5]) perText += perGroup[5].replace("M"," minutes ");
if(perGroup[6]) perText += perGroup[6].replace("S"," seconds ");
 
alert(perText);
</script>


0
 
ZvonkoSystems architectCommented:
Or like this:

<script>

var thePeriod = "P2DT23H32M51S";

var perGroup = thePeriod.match(/P((\d+)Y)?((\d+)M)?((\d+)D)?T((\d+)H)?((\d+)M)?((\d+)S)?/);
perText = "";

if(perGroup[1]) perText += perGroup[2]+" years ";
if(perGroup[3]) perText += perGroup[4]+" months ";
if(perGroup[5]) perText += perGroup[6]+" days ";
if(perGroup[7]) perText += perGroup[8]+" hors ";
if(perGroup[9]) perText += perGroup[10]+" minutes ";
if(perGroup[11]) perText += perGroup[12]+" seconds ";
 
alert(perText);
</script>



0
 
anyoneisCommented:
Well, this is quite ugly, but it does use regular expressions and has the flexibility I think you are looking for:

[Code]
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RegExExample
{
    class Program
    {
        //I need a regex that converts the ISO 8601 format duration string into a user readable string.
        //The format is: PnYnMnDTnHnMnS where
        //P  = period delimiter
        //Y = years
        //M = Months
        //D = Days
        //T = Time delimiter
        //H = hours
        //M = minutes
        //S = Seconds

        //So for the input string "P2DT23H32M51S" the result should be
        // "2 days 23 hours 32 minutes 51 seconds"

        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            do
            {

                MatchCollection mc = Regex.Matches(input,
                "(?:P(?:(?<years>[0-9]+)Y)?(?:(?<months>[0-9]+)M)?(?:(?<days>[0-9]+)D)?)?" +
                "(?:T(?:(?<hours>[0-9]+)H)?(?:(?<minutes>[0-9]+)M)?(?:(?<seconds>[0-9]+)S)?)?");

                if (mc.Count > 0)
                {
                    Match m = mc[0];
                    bool notFirst = false;

                    bool success;
                    string name;
                    string output = "";

                    name = "years";
                    success = m.Groups[name].Success;
                    output += success && notFirst ? " " : "";
                    output += (success) ? m.Groups[name].Value + " " + name : "";
                    notFirst = notFirst || success;

                    name = "months";
                    success = m.Groups[name].Success;
                    output += success && notFirst ? " " : "";
                    output += (success) ? m.Groups[name].Value + " " + name : "";
                    notFirst = notFirst || success;

                    name = "days";
                    success = m.Groups[name].Success;
                    output += success && notFirst ? " " : "";
                    output += (success) ? m.Groups[name].Value + " " + name : "";
                    notFirst = notFirst || success;

                    name = "hours";
                    success = m.Groups[name].Success;
                    output += success && notFirst ? " " : "";
                    output += (success) ? m.Groups[name].Value + " " + name : "";
                    notFirst = notFirst || success;

                    name = "minutes";
                    success = m.Groups[name].Success;
                    output += success && notFirst ? " " : "";
                    output += (success) ? m.Groups[name].Value + " " + name : "";
                    notFirst = notFirst || success;

                    name = "seconds";
                    success = m.Groups[name].Success;
                    output += success && notFirst ? " " : "";
                    output += (success) ? m.Groups[name].Value + " " + name : "";
                    notFirst = notFirst || success;

                    Console.WriteLine(output);
                }
                else
                {
                    Console.WriteLine("Invalid Format");
                }
                input = Console.ReadLine();
            } while (input.Length > 0);
        }
    }
}
[/Code]

David
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now