Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Regular Expression Replacement

  • 06-07-2011 4:28pm
    #1
    Registered Users, Registered Users 2 Posts: 357 ✭✭


    I have a basic enough question regarding regular expression

    I have a number that begins with 8 and that may have any number of following digits

    I want to be able to replace that first digit (which will always be 8) with a 5.

    The code I have so far to extract the 8 is
    ^(8)\d+
    

    Can anyone describe to me how to change this first digit to a 5?


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    apoch632 wrote: »
    I have a basic enough question regarding regular expression

    I have a number that begins with 8 and that may have any number of following digits

    I want to be able to replace that first digit (which will always be 8) with a 5.

    The code I have so far to extract the 8 is
    ^(8)\d+
    

    Can anyone describe to me how to change this first digit to a 5?
    We aren't psychic you know! What language are you working with?


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    Im psychic. Its ruby he is referring to. :cool:


  • Registered Users, Registered Users 2 Posts: 255 ✭✭boblong


    syklops wrote: »
    Im psychic. Its ruby he is referring to. :cool:

    Really?

    I'm not cool enough to know Ruby, but would a simple:
    >> a = "8544321"
    => "8544321"
    >> a.sub(/8/,"5")
    => "5544321"
    

    ... not work? I'm don't know if your "numbers" are strings or whatever or why you want to use a regex but there you go - "gsub" should replace ALL "8"s with "5"s if that's what you want.

    Others:
    >> a.sub(/./,"5")
    => "5544821"
    


    Depending on what it is you want exactly.

    Sorry if I've misinterpreted your question, I'm a bit tired :/


  • Registered Users, Registered Users 2 Posts: 357 ✭✭apoch632


    My apologies, it is ruby I am using

    I don't want to replace all instances of 8 just the first one


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    If 8 will always be the first number why don't you just replace it without using reg expressions.

    Don't know ruby but

    str[0] = "5"?

    Or am I mis reading your question.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 255 ✭✭boblong


    apoch632 wrote: »
    My apologies, it is ruby I am using

    I don't want to replace all instances of 8 just the first one

    Regexes are a bit of an odd fit, but yes using sub() will only replace the first '8'. gsub() will replace all of the '8's.


  • Registered Users, Registered Users 2 Posts: 297 ✭✭stesh


    boblong wrote: »
    Regexes are a bit of an odd fit, but yes using sub() will only replace the first '8'. gsub() will replace all of the '8's.

    With the first '8' not necessarily being the first digit of the number.


Advertisement