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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:18:12+00:00 2026-06-08T19:18:12+00:00

I am dynamically editing a regex for matching text in a pdf, which can

  • 0

I am dynamically editing a regex for matching text in a pdf, which can contain hyphenation at the end of some lines.

Example:

Source string:

"consecuti?vely"

Replace rules:

 .Replace("cuti?",@"cuti?(-\s+)?")
 .Replace("con",@"con(-\s+)?")
 .Replace("consecu",@"consecu(-\s+)?")

Desired output:

"con(-\s+)?secu(-\s+)?ti?(-\s+)?vely"

The replace rules are built dynamically, this is just an example which causes problems.

Whats the best solution to perform such a multiple replace, which will produce the desired output?

So far I thought about using Regex.Replace and zipping the word to replace with optional (-\s+)? between each character, but that would not work, because the word to replace already contains special-meaning characters in regex context.

EDIT: My current code, doesnt work when replace rules overlap like in example above

private string ModifyRegexToAcceptHyphensOfCurrentPage(string regex, int searchedPage)
    {
        var originalTextOfThePage = mPagesNotModified[searchedPage];
        var hyphenatedParts = Regex.Matches(originalTextOfThePage, @"\w+\-\s");
        for (int i = 0; i < hyphenatedParts.Count; i++)
        {
            var partBeforeHyphen = String.Concat(hyphenatedParts[i].Value.TakeWhile(c => c != '-'));

            regex = regex.Replace(partBeforeHyphen, partBeforeHyphen + @"(-\s+)?");
        }
        return regex;
    }
  • 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-08T19:18:15+00:00Added an answer on June 8, 2026 at 7:18 pm

    I had to give up an easy solution and did the editing of the regex myself. As a side effect, the new approach goes only twice trough the string.

    private string ModifyRegexToAcceptHyphensOfCurrentPage(string regex, int searchedPage)
        {
            var indexesToInsertPossibleHyphenation = GetPossibleHyphenPositions(regex, searchedPage);
            var hyphenationToken = @"(-\s+)?";
            return InsertStringTokenInAllPositions(regex, indexesToInsertPossibleHyphenation, hyphenationToken);
        }
    
        private static string InsertStringTokenInAllPositions(string sourceString, List<int> insertionIndexes, string insertionToken)
        {
            if (insertionIndexes == null || string.IsNullOrEmpty(insertionToken)) return sourceString;
    
            var sb = new StringBuilder(sourceString.Length + insertionIndexes.Count * insertionToken.Length);
            var linkedInsertionPositions = new LinkedList<int>(insertionIndexes.Distinct().OrderBy(x => x));
            for (int i = 0; i < sourceString.Length; i++)
            {
                if (!linkedInsertionPositions.Any())
                {
                    sb.Append(sourceString.Substring(i));
                    break;
                }
                if (i == linkedInsertionPositions.First.Value)
                {
                    sb.Append(insertionToken);
                }
                if (i >= linkedInsertionPositions.First.Value)
                {
                    linkedInsertionPositions.RemoveFirst();
                }
                sb.Append(sourceString[i]);
            }
            return sb.ToString();
        }
    
        private List<int> GetPossibleHyphenPositions(string regex, int searchedPage)
        {
            var originalTextOfThePage = mPagesNotModified[searchedPage];
            var hyphenatedParts = Regex.Matches(originalTextOfThePage, @"\w+\-\s");
            var indexesToInsertPossibleHyphenation = new List<int>();
            //....
            // Aho-Corasick to find all occurences of all 
            //strings in "hyphenatedParts" in the "regex" string
            // ....
            return indexesToInsertPossibleHyphenation;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am attempting to dynamically create html which then injects itself as an iframe into
I'm attempting to dynamically load a select box's options based on some existing criteria
I have searched high and low for an example of editing a parents children
Hi I have a DataGridView which is bound to an XML source. I have
Is there any way to implement text editing macros in SSMS? I would, e.g.
I have a dynamically templated gridview. It allows editing, deleting, and inserting new data
With ASP.NET MVC (or using HttpHandlers) you can dynamically generate URLs, like the one
I have a dynamically generated gridview with custom datasource. The gridview allows editing, inserting,
I'm dynamically loading User Controls into a div, which I need to preserve on
Dynamically I want to edit/update hosts(etc/hosts) file to add domain. To edit hosts(etc/hosts) file

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.