Solved

# Need a regular expresion

Posted on 2007-08-02
805 Views
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
Question by:astankovic

LVL 63

Expert Comment

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

LVL 63

Expert Comment

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 ");

</script>

0

LVL 63

Accepted Solution

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 ";

</script>

0

LVL 11

Expert Comment

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)
{
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");
}
} while (input.Length > 0);
}
}
}
[/Code]

David
0

## Featured Post

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…