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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:10:56+00:00 2026-06-11T13:10:56+00:00

Basically, I am trying to find the following pattern in a multiline textbox: [p]anyword

  • 0

Basically, I am trying to find the following pattern in a multiline textbox:

[p]anyword bla bla anyword[/p]

1.) The pattern can occur n-times in the textbox and I also want it n-times to be found.

2.) Between [p] and [/p] can be any character including whitespaces and linebreaks (“\r\n” in C#)

3.) I want the whole pattern, inluding the [p] and [/p]

The following code is very near to my wanted result. The problem is, that multiple linebreaks can occur between [p] and [/p]. I have tried out many many solutions. Nothing worked for me.

private void getTextFromTag2(String Tag, String txt)
{
    txt = txt.Replace("\r", "");

    string re1 = "(\\[";    
    string re2 = "p";   
    string re3 = "\\]"; 
    string re4 = ".*";  // Here lies the problem
    string re5 = "";    // Left open for a solution => \r\n cann occur n-times
    string re6 = "\\["; 
    string re7 = "\\/"; 
    string re8 = "p";   
    string re9 = "\\])";    

    Regex r = new Regex(re1 + re2 + re3 + re4 + re5 + re6 + re7 + re8 + re9, RegexOptions.IgnoreCase | RegexOptions.Multiline);

    MatchCollection mc = r.Matches(txt, 0);

    foreach (Match match in mc)
    {
        String c1 = match.Groups[1].ToString();
        Console.Write(c1 + "\r\n");
    }

}

As you might see, I already replaced “\r” with “” in txt, because the RegEx engine of .NET seems to want only “\n” as a new line character.

I think, the problem in my code is to be found in re4 and re5. re4 finds any character and works good, as long as there are no line breaks.

I think, re4 should say “any character, including whitespaces and \n”. But I really don’t get it.

So once again: Everting works fine, even if the pattern occurs many times in the textbox. The problem is, when linebreaks occur between [p] and [/p]

Here is an examle that does NOT work

[p]BlaBla BlaBla \r\n
BlaBla BlaBla \r\n
\r\n
BlaBla
[/p]

Here is an examle that DOES work

[p]BlaBla BlaBla[/p]
\r\n
\r\n
[p]Even more BlaBla[/p]
\r\n
\r\n
[p]Much more BlaBla[/p]

Please excuse my english. I am not a native english speaker.

Thank you.

This is the code, that now works for me. The changed things are //Changed Tagged

private void getTextFromTag2(String Tag, String txt)
    {
        //txt = txt.Replace("\r", ""); //Changed

        string re1 = "(\\[";     
        string re2 = "p";    
        string re3 = "\\]";  
        string re4 = ".*";   
        string re5 = "?";   // Changed
        string re6 = "\\["; 
        string re7 = "\\/"; 
        string re8 = "p";   
        string re9 = "\\])";    

        Regex r = new Regex(re1 + re2 + re3 + re4 + re5 + re6 + re7 + re8 + re9, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline); //Changed

        MatchCollection mc = r.Matches(txt, 0);

        foreach (Match match in mc)
        {
            String c1 = match.Groups[1].ToString();
            Console.Write(c1 + "\r\n");
        }

    }

Thank you so much.

  • 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-06-11T13:10:58+00:00Added an answer on June 11, 2026 at 1:10 pm

    You need to specify the Singleline option

    Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

    Basically the “Dot-matches-all” option you may be familiar with from other languages. The Multiline option you set only affects the behavior of the matching the beginning and ending of a line. See RegexOption class for more details.

    Regex r = new Regex(regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
    

    The other issue with the regex you provided is the * is greedy. so [p][/p][p][/p] would be a single match (it matched on the first [p] and the last [/p]. changing your re5 to:

    string re5 = "?";    // Non-greedy match on *
    

    Will fix that so you get two seperate matches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically i am trying to find out what version of ArcGIS the user currently
I have the following js which is basically to find the x and y
I have been trying to find a tutorial to do the following: I have
I've searched but didn't quite find something for my case. Basically, I'm trying to
I have a little issue I'm trying to find a solution for. Basically, imagine
tried to find the problem for this but nothings really working. Basically, I'm trying
I have the following slideshow im trying to build from scratch. http://jsfiddle.net/FeqkB/ Basically I
I'm trying to basically trying to separate a specific amount of text from the
I'm basically trying to iterate through a dict and print out the key /
I'm basically trying to extract a Mac application from a zip file using QuaZip.

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.