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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:22:12+00:00 2026-06-15T14:22:12+00:00

I am doing some text normalization using python and regular expressions. I would like

  • 0

I am doing some text normalization using python and regular expressions. I would like to substitute all ‘u’or ‘U’s with ‘you’.
Here is what I have done so far:

import re
text = 'how are u? umberella u! u. U. U@ U# u '
print re.sub (' [u|U][s,.,?,!,W,#,@ (^a-zA-Z)]', ' you ', text)

The output I get is:

how are you  you berella you  you  you  you  you  you

As you can see the problem is that ‘umberella’ is changed to ‘berella’. Also I want to keep the character that appears after a ‘u’. For example I want ‘u!’ to be changed to ‘you!’. Can anyone tell me what I am doing wrong and what is the best way to write the regular expression?

  • 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-15T14:22:14+00:00Added an answer on June 15, 2026 at 2:22 pm

    Firstly, why doesn’t your solution work. You mix up a lot of concepts. Mostly character class with other ones. In the first character class you use | which stems from alternation. In character classes you don’t need the pipe. Just list all characters (and character ranges) you want:

    [Uu]
    

    Or simply write u if you use the case-insensitive modifier. If you write a pipe there, the character class will actually match pipes in your subject string.

    Now in the second character class you use the comma to separate your characters for some odd reason. That does also nothing but include commas into the matchable characters. s and W are probably supposed to be the built-in character classes. Then escape them! Otherwise they will just match literal s and literal W. But then \W already includes everything else you listed there, so a \W alone (without square brackets) would have been enough. And the last part (^a-zA-Z) also doesn’t work, because it will simply include ^, (, ) and all letters into the character class. The negation syntax only works for entire character classes like [^a-zA-Z].

    What you actually want is to assert that there is no letter in front or after your u. You can use lookarounds for that. The advantage is that they won’t be included in the match and thus won’t be removed:

    r'(?<![a-zA-Z])[uU](?![a-zA-Z])'
    

    Note that I used a raw string. Is generally good practice for regular expressions, to avoid problems with escape sequences.

    These are negative lookarounds that make sure that there is no letter character before or after your u. This is an important difference to asserting that there is a non-letter character around (which is similar to what you did), because the latter approach won’t work at the beginning or end of the string.

    Of course, you can remove the spaces around you from the replacement string.

    If you don’t want to replace u that are next to digits, you can easily include the digits into the character classes:

    r'(?<![a-zA-Z0-9])[uU](?![a-zA-Z0-9])'
    

    And if for some reason an adjacent underscore would also disqualify your u for replacement, you could include that as well. But then the character class coincides with the built-in \w:

    r'(?<!\w)[uU](?!\w)'
    

    Which is, in this case, equivalent to EarlGray’s r'\b[uU]\b'.

    As mentioned above you can shorten all of these, by using the case-insensitive modifier. Taking the first expression as an example:

    re.sub(r'(?<![a-z])u(?![a-z])', 'you', text, flags=re.I)
    

    or

    re.sub(r'(?<![a-z])u(?![a-z])', 'you', text, flags=re.IGNORECASE)
    

    depending on your preference.

    I suggest that you do some reading through the tutorial I linked several times in this answer. The explanations are very comprehensive and should give you a good headstart on regular expressions, which you will probably encounter again sooner or later.

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

Sidebar

Related Questions

My particular scenario involves doing some text transformation using regular expressions within a private
I am doing some text parsing using flip-flop operator and my data looks like
I am doing some fairly extensive string manipulations using regular expressions in Java. Currently,
I am doing some regular expressions in php and matching using the preg_match(); I
I'm doing some personal research into text analysis, and have come up with close
I'm working in C# doing some OCR work and have extracted the text I
I have some text that looks like: California(2342) My object has a property that
I am doing some basic file manipulations, and would like to store the name
I'm doing: alert($(#div).text()); on something like this: <div id=div> &lt;div&gt; Some text &lt;div&gt; </div>
I am doing some text processing on a unix system. I have access to

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.