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 6150937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:41:43+00:00 2026-05-23T19:41:43+00:00

I’m searching for the best way to add a string to an existing string

  • 0

I’m searching for the best way to add a string to an existing string while I don’t want to replace the whole string.

self.fields_desc.append(BitField("foo", 0x3, 4))

sould be replaced by:

self.fields_desc.append(BitField("foo" + str(self.__class__.i), 0x3, 4))

Using which tool would allow me to do this with the less possible trouble? In vim I could do:

:%s/self.fields_desc.append(BitField("[a-zA-Z0-9]*", 0x[0-9]*, [0-9]*))/self.fields_desc.append(BitField("foo" + str(self.__class__.i), 0x3, 4))/g

But I don’t know how I would tell vim to not replace the regex I wrote. Could you give me a hand on this please?

  • 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-05-23T19:41:44+00:00Added an answer on May 23, 2026 at 7:41 pm

    Use capturing groups (note the “\” before the “(” and “)”, and the “\1”, “\2” etc):

    :%s/self\.fields_desc\.append(BitField(\("[a-zA-Z0-9]*"\), \(0x[0-9]\+\), \([0-9]\+\)))/self.fields_desc.append(BitField(\1 + str(self.__class__.i), \2, \3))/g
    

    changes:

    self.fields_desc.append(BitField("foo", 0x3, 4))
    self.fields_desc.append(BitField("test", 0x5, 3))
    

    to

    self.fields_desc.append(BitField("foo" + str(self.__class__.i), 0x3, 4))
    self.fields_desc.append(BitField("test" + str(self.__class__.i), 0x5, 3))
    

    Note:

    1. I’ve escaped the “.”, as “.” matches any character (except newline), and you want a literal “.” character.
    2. I’ve replaced * with + for the number matches: I doubt you want to match self.fields_desc.append(BitField("foo", 0x,) etc.
    3. If you aren’t sure whether the spacing is correct, i.e., you don’t always have self.fields_desc.append(BitField("foo", 0x3... but sometimes self.fields_desc.append(BitField("foo",0x3 or self.fields_desc.append(BitField("foo", 0x3, then add a * after the space characters. Although I’d suggest insteading standardising your code.

    See Regex grouping and The regex “dot”.


    As sidyll says, it is probably better to learn to use the built-in character classes “\d”, “\w” (see Shorthand character classes) and so on:

    :%s/self\.fields_desc\.append(BitField(\("\w*"\), \(0x\d\+\), \(\d\+\)))/self.fields_desc.append(BitField(\1 + str(self.__class__.i), \2, \3))/g
    

    This is both for brevity, and readability. Also, otherwise, readers will assume you have some special reason for defining your own character class (i.e., they will read it twice to make sure there’s not some unknown character in there).

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into

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.