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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:30:44+00:00 2026-05-28T08:30:44+00:00

I am in a strict Java environment. So the question is not really as

  • 0

I am in a strict Java environment.

So the question is not really as simple as in the tite, I am not trying to solve a problem I have, it is more theoretical for better knowledge.

What I am interested in is matching against src with either double or simple quote, but if it is double quote, it also has to be closed with a double quote, and same applies to simple quote.

I am aware of that i can repeat the regex in itself, ie:

String str = "src=\"hello/\" ... src='hello/' ..."

println str.replaceAll ("src=((\"[^\"]+\")|('[^']+'))", "src=$1")

What I would like to do is like:

println s.replaceAll ("src=([\"'][^\"']+[\"'])", "src=$1")

However, if it starts with double quote, then simple quotes should be allowed in the content, and it must end with a double quote, not a simple quote.

Question 2:

Is it possible to have it replaceAll with the same type of quote that was found?
Is it possible to say, for this match, replace with this2, for that, replace with that2.
How can you accomplish this without generating a new string each time?

Edit for Alan More, example for question two:

println "one ... two".replaceAll( "(one)", "1" ).replaceAll("(two)", "2");

more along these lines ( not right )

println "one ... two".replaceMyMatches( "(one)[^\\w]+(two)", "\$1{1}, \$2{2}" ) // prints string : one{1}, two{2} 

What I want is the string: 1, 2

Answer for first question derived and altered a bit from black panda and Jeff Walker:

String str = "src=\"1.png\" ... src='2.jpeg' ... src=\"3.p'ng\" ... src='4.jpe\"g' ... src='' ... src=\"\" ..." ;

String regex = "src=(['\"])(.+?)\\1"; // closes with the quote that is in group 1

println str.replaceAll( regex, '''src=$1../new_path/$2$1''')

Spits out:

src="../new_path/1.png" ... src='../new_path/2.jpeg' ... src="../new_path/3.p'ng" ... src='../new_path/4.jpe"g' ... src='' ... src="" ...

If one wants to replace the empty ones as well, just switch the + in the regex against a star ( I don’t want that )

Notice the original quotes are in as well.

Answer question two see black panda

  • 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-28T08:30:45+00:00Added an answer on May 28, 2026 at 8:30 am

    My answer to question 1 was originally incorrect. Here’s an updated version.

    To answer question 1..See if this regex helps you:
    The pattern is:

    src=(['"])(.*?)\1
    

    The code below explains each piece.

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Regex {
    
       public static void main(String[] args)
       {
          final String regex = "src=(['\"])" // the ' or the " is in group 1
                  + "(.*?)" // match any character in a non-greedy fashion
                  + "\\1"; // closes with the quote that is in group 1
          Pattern p = Pattern.compile(regex);
    
          Matcher m = p.matcher("src=\"hello/\"  ...   src='goodbye/'  ... "
                  + "src='this has a \" in it'");
    
          while (m.find())
          {
             System.out.println("\nfound!");
             System.out.println("The quote was a " + m.group(1));
             System.out.println("the text was = " + m.group(2));
          }
       }
    }
    

    This gives the output:

    found!
    The quote was a "
    the text was = hello/
    
    found!
    The quote was a '
    the text was = goodbye/
    
    found!
    The quote was a '
    the text was = this has a " in it
    

    As for the second question, you’ll have to use a little more code than that. You create your own StringBuffer and append as you go along. I used a map to hold the replacements:

       public static void question2()
       {
          Pattern p = Pattern.compile("one|two");
          Map<String, String> replacements = new HashMap<String, String>();
    
          replacements.put("one", "1");
          replacements.put("two", "2");
    
          StringBuffer result = new StringBuffer();
    
          String text = "one ... two";
    
          Matcher m = p.matcher(text);
    
          while (m.find())
          {
             m.appendReplacement(result, replacements.get(m.group()));
          }
    
          m.appendTail(result);
    
          System.out.println(result.toString());
    
       }
    

    This outputs:

    1 ... 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

do you know any not strict xpath for java? (I want it to not
I'm trying to ensure that my Rhino scripts (running under Java 6) are strict
Is there a good, strict date parser for Java? I have access to Joda-Time
I've run up on a really frustrating problem involving OS X and Java Swing
Having a background in Java, which is very verbose and strict, I find the
I have an XHTML strict page that has an invisible div that is controlled
In XHTML Strict, it seems that you're not allowed to use the <u> tag
I keep running into slight variations of a problem in Java and it's starting
I am trying to configure maven 3.0.3 for windows 7. I have it on
I have a very simple agent, basically just the required Agent_OnLoad method signature. If

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.