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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:17:49+00:00 2026-05-24T07:17:49+00:00

I need help on some regex problem with chinese characters in python. 拉柏多公园 is

  • 0

I need help on some regex problem with chinese characters in python.

“拉柏多公园” is the correct form of the word, but in a text i found “拉柏 多公 园”, what regex should i use to replace the characters.

import re

name = "拉柏多公园"
line = "whatever whatever it is then there comes a 拉柏 多公 园 sort of thing"
line2 = "whatever whatever it is then there comes another拉柏 多公 园 sort of thing"
line3 = "whatever whatever it is then there comes yet another 拉柏 多公 园sort of thing"
line4 = "whatever whatever it is then there comes a拉柏 多公 园sort of thing"

firstchar = "拉"
lastchar = "园"

i need to replace the strings in the lines so that the output line will look like this

line = "whatever whatever it is then there comes a 拉柏多公园 sort of thing"
line2 = "whatever whatever it is then there comes another 拉柏多公园 sort of thing"
line3 = "whatever whatever it is then there comes yet another 拉柏多公园 sort of thing"
line4 = "whatever whatever it is then there comes a 拉柏多公园 sort of thing"

i tried these to but the regex is badly structured:

reline = line.replace (r"firstchar*lastchar", name) #
reline2 = reline.replace ("  ", " ")
print reline2

can someone help to correct my regex?

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-24T07:17:50+00:00Added an answer on May 24, 2026 at 7:17 am

    (I assume you’re using python 3, since you’re using unicode characters in regular strings. For python 2, add u before each string literal.)

    Python 3

    import re
    
    name = "拉柏多公园"
    # the string of Chinese characters, with any number of spaces interspersed.
    # The regex will match any surrounding spaces.
    regex = r"\s*拉\s*柏\s*多\s*公\s*园\s*"
    

    So you can replace each string with

    reline = re.sub(regex, ' ' + name + ' ', line)
    

    Python 2

    # -*- coding: utf-8 -*-
    
    import re
    
    name = u"拉柏多公园"
    # the string of Chinese characters, with any number of spaces interspersed.
    # The regex will match any surrounding spaces.
    regex = ur"\s*拉\s*柏\s*多\s*公\s*园\s*"
    

    So you can replace each string with

    reline = re.sub(regex, u' ' + name + u' ', line)
    

    Discussion

    The result will be surrounded by spaces. More generally, if you want it to work at the start or end of the line, or before commas or periods, you’ll have to replace ' ' + name + ' ' with something more sophisticated.

    Edit: fixed. Of course, you have to use the re library function.

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

Sidebar

Related Questions

I need some help on Regex. I need to find a word that is
being a regex beginner, I need some help writing a regex. It should match
I need a some help to replace all non-word characters in a string. As
I need some help using regex in java. Im trying to achieve 2 different
I need some help writing a regex. I have the following strings, xxx.yyy.wwwwwaaa_IN_123 xxx.rrrttttt_IN_12355
I need some help. I am looking for a regex that would match the
I need help debugging some odd file behavior in Python. Take the following script
I need help with a RegEx problem: I want to find occurences of two
I need some Regex help. I need to find beacon_ followed by an alphanumeric
Need some help with regex matching please. I'm trying to match a double quoted

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.