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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:39:59+00:00 2026-05-16T15:39:59+00:00

How can I replace following string in Java: Sports videos (From 2002 To 2003)

  • 0

How can I replace following string in Java:

Sports videos (From 2002 To 2003) here.

TO

Sports videos 2002 2003 here.

I have use code but it remove the whole string i.e.
I am getting this ouput: Sports videos here.

String pattern= "\\((From)(?:\\s*\\d*\\s*)(To)(?:\\s*\\d*\\s*)\\)";

String testStr = "Sports videos (From 2002 To 2003) here.";

String testStrAfterRegex =  testStr.replaceFirst(pattern, "");

What is missing here?

Thanks

DIFFERENT STRING WITH DATE FORMATTER

If above string has date formatter like(\\) or any other character/words then digit, the answer will not work

I replace orginal answer with this pattern and it will work

String pattern= "\\((From)(.*)(To)(.*)\\)";
  • 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-16T15:39:59+00:00Added an answer on May 16, 2026 at 3:39 pm

    Change to

        String pattern= "\\((From)(\\s*\\d*\\s*)(To)(\\s*\\d*\\s*)\\)";
        String testStr = "Sports videos (From 2002 To 2003) here.";
        String testStrAfterRegex =  testStr.replaceFirst(pattern, "$2 $4");
    

    There are two problems:

    First

    You put (?:) in groups with years. This is used to not remember these groups.

    Second

    You don’t use group identifiers, like $1, $2.
    I fixed using $2 and $4 for 2th and 4th groups.


    EDIT

    Cleaner solution:

        String pattern= "\\(From(\\s*\\d*\\s*)To(\\s*\\d*\\s*)\\)";
        String testStr = "Sports videos (From 2002 To 2003) here.";
        String testStrAfterRegex =  testStr.replaceFirst(pattern, "$1$2");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that can replace the email address in a String
can you help me with a function to replace string as following: The string
I have the following formula: =ArrayFormula(SUM(Morten!$M4:$M400)) How can I replace Morten with the contents
I need to know how I can replace the last s from a string
How can I use a Servlet Filter to change an incoming URL from http://nm-java.appspot.com/Check_License/Dir_My_App/Dir_ABC/My_Obj_123
I have the following string and would like to use str_replace or preg_replace to
I have the following string ListId={1CC88B01-E60F-45D1-8B3C-28852574156D}&ID={0}&ContentTypeID=0x01003D458D19EF31D845B3A7727B0F2F8FC8 I would like to use it as such
I have the following java method, written for an android application. private String removeWifiFudge(String
This is my replace method in java but unble to replace for following. It
I'm trying the following code in Java in which I need to replace a

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.