Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:57:16+00:00 2026-06-04T16:57:16+00:00

I would like to capitalize everything in a character vector that comes after the

  • 0

I would like to capitalize everything in a character vector that comes after the first _. For example the following vector:

x <- c("NYC_23df", "BOS_3_rb", "mgh_3_3_f") 

Should come out like this:

"NYC_23DF" "BOS_3_RB" "mgh_3_3_F"

I have been trying to play with regular expressions, but am not able to do this. Any suggestions would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:57:18+00:00Added an answer on June 4, 2026 at 4:57 pm

    You were very close:

    gsub("(_.*)","\\U\\1",x,perl=TRUE)
    

    seems to work. You just needed to use _.* (underscore followed by zero or more other characters) rather than _* (zero or more underscores) …

    To take this apart a bit more:

    • _.* gives a regular expression pattern that matches an underscore _ followed by any number (including 0) of additional characters; . denotes “any character” and * denotes “zero or more repeats of the previous element”
    • surrounding this regular expression with parentheses () denotes that it is a pattern we want to store
    • \\1 in the replacement string says “insert the contents of the first matched pattern”, i.e. whatever matched _.*
    • \\U, in conjunction with perl=TRUE, says “put what follows in upper case” (uppercasing _ has no effect; if we wanted to capitalize everything after (for example) a lower-case g, we would need to exclude the g from the stored pattern and include it in the replacement pattern: gsub("g(.*)","g\\U\\1",x,perl=TRUE))

    For more details, search for “replacement” and “capitalizing” in ?gsub (and ?regexp for general information about regular expressions)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to capitalize the first letter following a colon or an ? in
I would like to change case (all caps or capitalize first letter of sentence)
I would like to capitalize names properly, which in this case means: The first
Would like to sort the following XML by joining the fsxml/formula/ingrrow where itemcode =
Would like to make anapplication in Java that will not automatically parse parameters used
Would like to know how to integarate cruise control with maven? Cruise Control comes
Would like to know how to hide an div after a set of css3
Suppose I have a table that contains valid data. I would like to modify
I would like to change the following string fgsloiwrthowmwohitjwptpbspdfmjlsihjwslihj to this string FGsloIwrtHowMwoHItJwpTpBspDfmJlsIhJwslIHJ I
Is there a function built into Java that capitalizes the first character of each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.