Exclusion via regex

Asked

Viewed 175 times

1

I have the following string:

2014-10-12 17:04:29.996

It’s a format timestamp and is wrong because after the hour there is the .996. To capture this stretch I used the regex :

\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{2,3}

How can I remove only the ending taking into account that I have many occurrences ?

  • vc opened the xml in a text editor and wants to remove the microsecond?

  • Exactly that, I may have misexplained the question.

  • I need to save the first part to replace it by removing what I don’t want.

  • The answer didn’t resolve? your string is between tags?

  • It’s like this: DT_HR_ALTERACAO="2015-02-05 10:55:01.324"

3 answers

3


To capture the microsecond you can use the following regex \.\d+(")$ it says to capture at the end of the string a point followed by one or more digits followed by a double quote which is a group. In the substitution field put \1 or $1 is the value of the catch group in the case of the double quote.

=> ^ capture in the example:

DT_HR_ALTERACAO="2015-02-05 10:55:01.324"
                                    ^^^^^  

I imagine he’s captured to be replaced by something else.

  • As soon as I test I’ll get back to you, thank you.

2

In sublime: select Exp.regulates and:

find=    (DT_HR_ALTERACAO="\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\.\d{2,3}
replace= \1

At the command line:

perl -i.bak -pe 's/...find anterior.../$1/g' ex.xml

2

Using the sed gets like this:

sed -r 's/\.[0-9]{3}"$//' nome-arquivo >> novo-arquivo

Adapted from reply of @rray

  • by the way > novo-arquivo or else >> velho

Browser other questions tagged

You are not signed in. Login or sign up in order to post.