Help with regular expressions

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

Help with regular expressions

Fisher Dennis
R 3.4.2
OS X

Colleagues

I would appreciate some help with regular expressions.

I have string that looks like:
        " ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1,1)             ,SIGMA(2,1)             ,SIGMA(2,2)”

In the entries that contain:
        (X,Y) # for example, SIGMA(1,1)
I would like to replace the comma with a period, e.g., SIGMA(1.1) but NOT the other commas

The end-result would be:
        " ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1.1)             ,SIGMA(2.1)             ,SIGMA(2.2)”

Can someone provide the regular expression code to accomplish this?
Thanks.

Dennis

Dennis Fisher MD
P < (The "P Less Than" Company)
Phone / Fax: 1-866-PLessThan (1-866-753-7784)
www.PLessThan.com

______________________________________________
[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 expressions

Jim Lemon-4
Hi Dennis,
How about:


# define the two values to search for
x<-2
y<-3
# create your search string and replacement string
repstring<-paste(x,y,sep=",")
newstring<-paste(x,y,sep=".")
# this is the string that you want to change
thetastring<-"SIGMA(2,3)"
sub(repstring,newstring,thetastring)
[1] "SIGMA(2.3)"

Use gsub if you want to change multiple values

Jim

On Tue, Feb 13, 2018 at 1:22 PM, Dennis Fisher <[hidden email]> wrote:

> R 3.4.2
> OS X
>
> Colleagues
>
> I would appreciate some help with regular expressions.
>
> I have string that looks like:
>         " ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1,1)             ,SIGMA(2,1)             ,SIGMA(2,2)”
>
> In the entries that contain:
>         (X,Y)                   # for example, SIGMA(1,1)
> I would like to replace the comma with a period, e.g., SIGMA(1.1) but NOT the other commas
>
> The end-result would be:
>         " ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1.1)             ,SIGMA(2.1)             ,SIGMA(2.2)”
>
> Can someone provide the regular expression code to accomplish this?
> Thanks.
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
> ______________________________________________
> [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 expressions

David Winsemius
In reply to this post by Fisher Dennis

> On Feb 12, 2018, at 6:22 PM, Dennis Fisher <[hidden email]> wrote:
>
> R 3.4.2
> OS X
>
> Colleagues
>
> I would appreciate some help with regular expressions.
>
> I have string that looks like:
> " ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1,1)             ,SIGMA(2,1)             ,SIGMA(2,2)”
>
> In the entries that contain:
> (X,Y) # for example, SIGMA(1,1)
> I would like to replace the comma with a period, e.g., SIGMA(1.1) but NOT the other commas
>
> The end-result would be:
> " ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1.1)             ,SIGMA(2.1)             ,SIGMA(2.2)”
>
> Can someone provide the regular expression code to accomplish this?

gsub( "([(]\\d+)([,])(\\d+[)])", "\\1.\\3", x)\
#-----------
[1] "ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6         ,THETA7                 ,SIGMA(1.1)             ,SIGMA(2.1)             ,SIGMA(2.2)"


> Thanks.
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
> ______________________________________________
> [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.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law

______________________________________________
[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 expressions

Boris Steipe
In reply to this post by Jim Lemon-4
You can either use positive lookahead/lookbehind - but support for that is a bit flaky. Or write a proper regex, and use
backreferences to keep what you need.

R > x <- "abc 1,1 ,1 1, x,y 2,3 "

R > gsub("(\\d),(\\d)", "\\1.\\2", x, perl = TRUE)
[1] "abc 1.1 ,1 1, x,y 2.3 "


B.



> On Feb 12, 2018, at 9:34 PM, Jim Lemon <[hidden email]> wrote:
>
> Hi Dennis,
> How about:
>
>
> # define the two values to search for
> x<-2
> y<-3
> # create your search string and replacement string
> repstring<-paste(x,y,sep=",")
> newstring<-paste(x,y,sep=".")
> # this is the string that you want to change
> thetastring<-"SIGMA(2,3)"
> sub(repstring,newstring,thetastring)
> [1] "SIGMA(2.3)"
>
> Use gsub if you want to change multiple values
>
> Jim
>
> On Tue, Feb 13, 2018 at 1:22 PM, Dennis Fisher <[hidden email]> wrote:
>> R 3.4.2
>> OS X
>>
>> Colleagues
>>
>> I would appreciate some help with regular expressions.
>>
>> I have string that looks like:
>>        " ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1,1)             ,SIGMA(2,1)             ,SIGMA(2,2)”
>>
>> In the entries that contain:
>>        (X,Y)                   # for example, SIGMA(1,1)
>> I would like to replace the comma with a period, e.g., SIGMA(1.1) but NOT the other commas
>>
>> The end-result would be:
>>        " ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1.1)             ,SIGMA(2.1)             ,SIGMA(2.2)”
>>
>> Can someone provide the regular expression code to accomplish this?
>> Thanks.
>>
>> Dennis
>>
>> Dennis Fisher MD
>> P < (The "P Less Than" Company)
>> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
>> www.PLessThan.com
>>
>> ______________________________________________
>> [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.

______________________________________________
[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 expressions

Ulrik Stervbo-2
I think I would replace all , with . and subsequently replace all first .
with , using ^\\.

x <- gsub(",", ".", x)
gsub("^\\.", ",", x)

It's not so elegant, but it is easier to understand than backreferences and
complex regex.

Best,
Ulrik

On Tue, 13 Feb 2018, 03:38 Boris Steipe, <[hidden email]> wrote:

> You can either use positive lookahead/lookbehind - but support for that is
> a bit flaky. Or write a proper regex, and use
> backreferences to keep what you need.
>
> R > x <- "abc 1,1 ,1 1, x,y 2,3 "
>
> R > gsub("(\\d),(\\d)", "\\1.\\2", x, perl = TRUE)
> [1] "abc 1.1 ,1 1, x,y 2.3 "
>
>
> B.
>
>
>
> > On Feb 12, 2018, at 9:34 PM, Jim Lemon <[hidden email]> wrote:
> >
> > Hi Dennis,
> > How about:
> >
> >
> > # define the two values to search for
> > x<-2
> > y<-3
> > # create your search string and replacement string
> > repstring<-paste(x,y,sep=",")
> > newstring<-paste(x,y,sep=".")
> > # this is the string that you want to change
> > thetastring<-"SIGMA(2,3)"
> > sub(repstring,newstring,thetastring)
> > [1] "SIGMA(2.3)"
> >
> > Use gsub if you want to change multiple values
> >
> > Jim
> >
> > On Tue, Feb 13, 2018 at 1:22 PM, Dennis Fisher <[hidden email]>
> wrote:
> >> R 3.4.2
> >> OS X
> >>
> >> Colleagues
> >>
> >> I would appreciate some help with regular expressions.
> >>
> >> I have string that looks like:
> >>        " ITERATION              ,THETA1                 ,THETA2
>          ,THETA3                 ,THETA4                 ,THETA5
>      ,THETA6                 ,THETA7                 ,SIGMA(1,1)
>  ,SIGMA(2,1)             ,SIGMA(2,2)”
> >>
> >> In the entries that contain:
> >>        (X,Y)                   # for example, SIGMA(1,1)
> >> I would like to replace the comma with a period, e.g., SIGMA(1.1) but
> NOT the other commas
> >>
> >> The end-result would be:
> >>        " ITERATION              ,THETA1                 ,THETA2
>          ,THETA3                 ,THETA4                 ,THETA5
>      ,THETA6                 ,THETA7                 ,SIGMA(1.1)
>  ,SIGMA(2.1)             ,SIGMA(2.2)”
> >>
> >> Can someone provide the regular expression code to accomplish this?
> >> Thanks.
> >>
> >> Dennis
> >>
> >> Dennis Fisher MD
> >> P < (The "P Less Than" Company)
> >> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> >> www.PLessThan.com
> >>
> >> ______________________________________________
> >> [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.
>
> ______________________________________________
> [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.
>

        [[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.