Help with Regular expression

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with Regular expression

Bogaso
Hi,

I was working on following expression :

"\":\"03-JAN-2018 16:00:00\""


This is basically a combination of Date and Time mixed with some Noise.

I want to extract only Date and Time part i.e. "03-JAN-2018 16:00:00

I tried following :

gsub("![0-9][0-9]-[a-zA-Z][a-zA-Z][a-zA-Z]-[0-9][0-9][0-9][0-9]
[0-9][0-9]:[0-9][0-9]:[0-9][0-9]", "", "\":\"03-JAN-2018 16:00:00\"",
ignore.case = TRUE)

Obviously, with above code, I am removing that portion of my string
which I actually I wanted!

How can I reverse above code, so that I will be removing that portion
of my string which I actually I ***NOT*** wanted?

Thanks for your time.

Happy New Year!

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Help with Regular expression

David Wolfskill
On Wed, Jan 03, 2018 at 07:56:27PM +0530, Christofer Bogaso wrote:

> Hi,
>
> I was working on following expression :
>
> "\":\"03-JAN-2018 16:00:00\""
>
>
> This is basically a combination of Date and Time mixed with some Noise.
>
> I want to extract only Date and Time part i.e. "03-JAN-2018 16:00:00
>
> I tried following :
>
> gsub("![0-9][0-9]-[a-zA-Z][a-zA-Z][a-zA-Z]-[0-9][0-9][0-9][0-9]
> [0-9][0-9]:[0-9][0-9]:[0-9][0-9]", "", "\":\"03-JAN-2018 16:00:00\"",
> ignore.case = TRUE)
>
> Obviously, with above code, I am removing that portion of my string
> which I actually I wanted!
>
> How can I reverse above code, so that I will be removing that portion
> of my string which I actually I ***NOT*** wanted?
> ....
You may find strptime() more suitable for the intended purpose.

Peace,
david
--
David H. Wolfskill [hidden email]
If you want the best Fake News, go to the best source of it: Donald J. Trump.

See http://www.catwhisker.org/~david/publickey.gpg for my public key.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (616 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Help with Regular expression

Rui Barradas
In reply to this post by Bogaso
Hello,

I believe the following regex will do it.

x <- "\":\"03-JAN-2018 16:00:00\""

sub('^.*(\\d{2}-\\w{3}-\\d{4} \\d{2}:\\d{2}:\\d{2})[:"]', '\\1', x)


Hope this helps,

Rui Barradas

On 1/3/2018 2:26 PM, Christofer Bogaso wrote:

> Hi,
>
> I was working on following expression :
>
> "\":\"03-JAN-2018 16:00:00\""
>
>
> This is basically a combination of Date and Time mixed with some Noise.
>
> I want to extract only Date and Time part i.e. "03-JAN-2018 16:00:00
>
> I tried following :
>
> gsub("![0-9][0-9]-[a-zA-Z][a-zA-Z][a-zA-Z]-[0-9][0-9][0-9][0-9]
> [0-9][0-9]:[0-9][0-9]:[0-9][0-9]", "", "\":\"03-JAN-2018 16:00:00\"",
> ignore.case = TRUE)
>
> Obviously, with above code, I am removing that portion of my string
> which I actually I wanted!
>
> How can I reverse above code, so that I will be removing that portion
> of my string which I actually I ***NOT*** wanted?
>
> Thanks for your time.
>
> Happy New Year!
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Help with Regular expression

Arun.stat
In reply to this post by Bogaso
Hi Bogaso,

I see your ultimate goal is to extract the Date-time part from your
expression, then below should help :

> as.POSIXlt(gsub("[^0-9a-zA-Z]", "", "\":\"03-JAN-2018 16:00:00\""),
format = "%d%b%Y%H%M%OS")

[1] "2018-01-03 16:00:00 GMT"
_____________________________________________________

Arun Kumar Saha, FRM
QUANTITATIVE RISK AND HEDGE CONSULTING SPECIALIST
 LinkedIn: http://in.linkedin.com/in/ArunFRM
 Personal : http://WWW.ARUNSAHA.IN  <http://WWW.ARUNSAHA.IN>
_____________________________________________________

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.