Skoota
Skoota

Reputation: 5290

Regex to remove decimal values

Given a timestamp in ISO 8601 format below:

2012-04-21T01:56:00.581550

what regular expression would remove the decimal point and the millisecond precision? In other words, a regex that applies to the above and returns the following:

2012-04-21T01:56:00

This is probably very simple, but not being particular familiar with regex I am unsure how to approach the solution. Thanks in advance for any assistance.

Upvotes: 1

Views: 23717

Answers (5)

Ωmega
Ωmega

Reputation: 43673

Code:

$_ = '2012-04-21T01:56:00.581550';
s/\.\d*//;
print $_, "\n";

Test:

http://ideone.com/52hij

Output:

2012-04-21T01:56:00

Upvotes: 0

0x90
0x90

Reputation: 40982

why do you want to use regex? use string operations

in python :

>>> "2012-04-21T01:56:00.581550".split(".")
['2012-04-21T01:56:00', '581550']
>>> "2012-04-21T01:56:00.581550".split(".")[0]
'2012-04-21T01:56:00'

Upvotes: 2

Tony Day
Tony Day

Reputation: 2170

This regex ^[\w\-:]+ will only match up to the period and excluding it. You can use this to find the part of the time-stamp you want.

  • ^ is the beginning of the string.
  • \w is any "word".
  • \- includes the hyphen.
  • : includes the colon.
  • These placed in [] means only matching these characters.
  • The + means matching one or many instances of those characters.

Since the period (.) is not included, the regex will stop matching when it gets to that.

Upvotes: 2

Donald Miner
Donald Miner

Reputation: 39893

s/\..*$//

It looks like you can assume there will only be one dot. The above sed expression finds a dot, then replaces everything after that dot up until the newline with nothing.

Without sed: replace \..*$ with the empty string ""

\. is the literal period (have to escape it because . means any character)

.* means any and all characters

$ means end of line

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726549

If you must use regex, you can use "[.][0-9]+$" and replace it with an empty string "". It is easier to locate the trailing '.', and chop off the string at its index. In C#, that would be

myStr = myStr.Substring(0, myStr.LastIndexOf('.')-1);

Upvotes: 3

Related Questions