Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

reuse expression

Last post 08-28-2008, 3:34 PM by qwerty53. 5 replies.
Sort Posts: Previous Next
  •  08-28-2008, 9:48 AM 45738

    reuse expression

    Hi,

    My regex is: (.*?\\|)(.*?\\|)(.*?\\|)(.*?\\|)(.*?\\|)(.*?\\|)(?:.*?\\|)(.*?\\|)(.*?\\|)(?:.*?\\|)(.*)

    so I have 9 matching groups.

    How do I shorten this and reuse the  (.*?\\|). I tried: (.*?\\|){6}(?:.*?\\|)(.*?\\|){2}(?:.*?\\|)(.*) but it doesn't give me the 9 matching groups.

     

    Thanks,

  •  08-28-2008, 9:56 AM 45739 in reply to 45738

    Re: reuse expression

    When you reduce the number of capture groups that is the result.

    Please provide source text and desired matches if you would like additional assistance.

    This is how I would reuse that match group pattern based on your example:

    (.*?\\|)((?1))((?1))((?1))((?1))((?1))(?1)((?1))((?1))(?1)(.*)


  •  08-28-2008, 10:17 AM 45740 in reply to 45739

    Re: reuse expression

    abc|abc|abc|abc|abc|abc|xxxxxx|abc|abc|zzzzzzzzzz|sdfjashdipfhaeofhaopsdfhabcwejsopfjpsdfj

     

    so if I apply my regex on this string and concatinate the 9 groups I'll just lose the x's and the z's in the middle

  •  08-28-2008, 10:19 AM 45741 in reply to 45740

    Re: reuse expression

    Confirm you want the resulting string to be:

    abc|abc|abc|abc|abc|abc|abc|abc|sdfjashdipfhaeofhaopsdfhabcwejsopfjpsdfj


  •  08-28-2008, 10:29 AM 45742 in reply to 45740

    Re: reuse expression

    Raw Match Pattern:
    (([^|]*\|){6})(?2)((?2){2})(?2)(.*)

    Concatenate groups 1, 3, 4.

    or:

    Raw Match Pattern:
    ((?:[^|]*\|){6})[^|]*\|((?:[^|]*\|){2})[^|]*\|(.*)

    Concatenate groups 1,2,3.

    Double \'s as required by your platform (i.e. Java).


  •  08-28-2008, 3:34 PM 45764 in reply to 45742

    Re: reuse expression

    Thank man. You are the king!!!!! (Although the first one is illegal syntax)
View as RSS news feed in XML