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

Matching sentence between two strings

Last post 08-28-2008, 11:29 AM by lightInt. 5 replies.
Sort Posts: Previous Next
  •  08-28-2008, 5:57 AM 45732

    Matching sentence between two strings

    Hi,

    I am trying to create a regex that will match a sentence between two specific strings.
    below is the sentence to match, the part I would like to match is marked in bold.

    [[Text:hi this is a nice sentence[[very nice]] I like this text]] this is not related [[not]]

    I have the following regex: \[\[Text:.*?]]
    The problem is that it stops at the ]] after "very nice" and matches only: [[Text:hi this is a nice sentence[[very nice]]

    Thanks anyone for any help.

    Light Interactive.

  •  08-28-2008, 8:06 AM 45735 in reply to 45732

    Re: Matching sentence between two strings

    What platform are you using?  Is this part of a replacement operation or just matching?

    This worked for me in PHP:

    Raw Match Pattern:
    (\[(?:(?>[^\[\]]+)|(?1))*\])

    Note that you will need to do a second test to match only the Text: matches returned.

    Note that another acceptable pattern for this nested [ ] matching would have also been:

    Raw Match Pattern:
    \[(?:(?>[^\[\]]+)|(?R))*\]

    However the pattern above using (?1) allows for the use of lookbehinds etc.


  •  08-28-2008, 10:55 AM 45746 in reply to 45735

    Re: Matching sentence between two strings

    I am usint .net, and this is part of a replacement string.

    I am trying to test the patterns you suggested in Expresso (I hope you know) but I get errors.

    thanks.

  •  08-28-2008, 11:00 AM 45747 in reply to 45746

    Re: Matching sentence between two strings

    VB or C# .NET?
  •  08-28-2008, 11:19 AM 45749 in reply to 45746

    Re: Matching sentence between two strings

    Here's C# .NET code:

    http://pastebin.com/f350a0748

    Example pattern adapted from Friedl's "Mastering Regular Expressions" page 436.


  •  08-28-2008, 11:29 AM 45750 in reply to 45749

    Re: Matching sentence between two strings

    Thank you very much for your help!
View as RSS news feed in XML