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

  • Home
  • SEARCH
  • 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 6623923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:35:25+00:00 2026-05-25T21:35:25+00:00

I have a corpus: txt = a patterned layer within a microelectronic pattern. I

  • 0

I have a corpus:
txt = “a patterned layer within a microelectronic pattern.”
I would like to replace the term “pattern” exactly by “form”, I try to write a code:

txt_replaced = gsub("pattern","form",txt)

However, the responsed corpus in txt_replaced is:
“a formed layer within a microelectronic form.”

As you can see, the term “patterned” is wrongly replaced by “formed” because parts of characteristics in “patterned” matched to “pattern”.

I would like to query that if I can replace the string exactly using gsub()?
That is, only the term with exactly match should be replaced.

I thirst for a responsed as below:
“a patterned layer within a microelectronic form.”

Many thanks!

  • 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-25T21:35:26+00:00Added an answer on May 25, 2026 at 9:35 pm

    As @koshke noted, a very similar question has been answered before (by me). …But that was grep and this is gsub, so I’ll answer it again:

    “\<” is an escape sequence for the beginning of a word, and “>” is the end. In R strings you need to double the backslashes, so:

    txt <- "a patterned layer within a microelectronic pattern."
    txt_replaced <- gsub("\\<pattern\\>","form",txt)
    txt_replaced
    # [1] "a patterned layer within a microelectronic form."
    

    Or, you could use \b instead of \< and \>. \b matches a word boundary so it can be used at both ends>

    txt_replaced <- gsub("\\bpattern\\b","form",txt)
    

    Also note that if you want to replace only ONE occurrence, you should use sub instead of gsub.

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

Sidebar

Related Questions

I have a fairly small corpus of structured records sitting in a database. Given
I have the following function: def storeTaggedCorpus(corpus, filename): corpusFile = codecs.open(filename, mode = 'w',
I have to search a 25 GB corpus of wikipedia for a single word.
Have you seen library for flexible working with terminal(Unix like)? I want to implement
I have a corpus of token-index based documents which offers a query method. The
I have an application that causes an OutOfMemoryError, so I try to debug it
I have a project in Java requiring to load large files (corpus for NLP)
Have the following scenario. I have a few form, which essentially have a few
I have a corpus of lyrics of Indian songs and need to tag them
I have a corpus of documents and I want to represent each document as

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.