Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Replace withing regex match

  1. #1
    Tim99 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Replace within regex match

    Hi,

    I have a large String text. With a regex, I want to look for a pattern {@link org.domain.this.is.my.class }. For each match, I want to replace the "." with the expression ":".

    This code almost solves my problem. However, it also replaces the dots in "org.domain.this.is.my.class" even if its outside a {@link ... } pattern. I know that line 7 is to blame. Yet, I don't know the answer :-( Any help is greatly appreciated!


    Java Code:
    		Pattern pattern = Pattern.compile("\\{@link (.*?)}");
    		Matcher matcher = pattern.matcher(text);
    		boolean moreOccurences = matcher.find();
    		while (moreOccurences) {
    			String origString = matcher.group(1);
    			String replString = matcher.group(1).replace(".", ":");
    			text = text.replace(origString, replString);
    			moreOccurences = matcher.find();
    		}
    Last edited by Tim99; 07-27-2012 at 03:54 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Replace withing regex match


  3. #3
    Tim99 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: Replace withing regex match

    Yes, indeed!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Replace withing regex match

    I'm normally against what we term 'spoonfeeding' but in the case of regexes there's really no way to guide someone to a solution :)

    So here goes ...
    Java Code:
    public class ReplaceDotsWithColons {
    
      public static void main(String[] args) {
        String input = "org.domain.this.is.my.class some text "
                + "{@link org.domain.this.is.my.class } "
                + "some text org.domain.this.is.my.class"
                + "{@link org.domain.this.is.my.class } "
                + "some text org.domain.this.is.my.class";
        String regex = "(?<=[{]@link \\S{1,999})\\.(?=\\S* [}])";
        String output = input.replaceAll(regex, ":");
        System.out.println(output);
      }
    }
    Note that the {1,999} is an overkill workaround; unlike some other languages, Java doesn't support lookbehinds that don't have a clearly defined maximum length.

    If you don't understand the regex, ask.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Replace withing regex match

    Quote Originally Posted by eRaaaa View Post
    So, it took me more than half an hour to figure out the regex :( Anyway, I think my solution is more compact.

    db
    awinston likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. regex replace all ignore case
    By nn12 in forum New To Java
    Replies: 2
    Last Post: 04-06-2011, 04:58 PM
  2. Using regex to replace characters in a string
    By DC200 in forum New To Java
    Replies: 7
    Last Post: 10-13-2010, 02:35 PM
  3. Regex search and replace
    By joe robles in forum Advanced Java
    Replies: 1
    Last Post: 09-25-2010, 02:32 PM
  4. JLabel is not refreshed from withing ActionPerformed
    By mavaforumsm in forum AWT / Swing
    Replies: 2
    Last Post: 05-25-2010, 05:32 PM
  5. A crazy gui match
    By amarenash23 in forum New To Java
    Replies: 8
    Last Post: 12-30-2009, 03:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •