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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:02:34+00:00 2026-05-12T10:02:34+00:00

The sad truth about this post is that I have poor regex skills. I

  • 0

The sad truth about this post is that I have poor regex skills. I recently came across some code in an old project that I seriously want to do something about. Here it is:

strDocument = strDocument.Replace("font size=""1""", "font size=0.2")
strDocument = strDocument.Replace("font size='1'", "font size=0.2")
strDocument = strDocument.Replace("font size=1", "font size=0.2")
strDocument = strDocument.Replace("font size=""2""", "font size=1.5")
strDocument = strDocument.Replace("font size='2'", "font size=1.5")
strDocument = strDocument.Replace("font size=2", "font size=1.5")
strDocument = strDocument.Replace("font size=3", "font size=2")
strDocument = strDocument.Replace("font size=""3""", "font size=2")
strDocument = strDocument.Replace("font size='3'", "font size=2")

I’m guessing there is some easy regex pattern out there that I could use to find different ways of quoting attribute values and replace them with valid syntax. For example if somebody wrote some HTML that looks like:

<tag attribute1=value attribute2='value' />

I’d like to be able to easily clean that tag so that it ends up looking like

<tag attribute1="value" attribute2="value" />

The web application I’m working with is 10 years old and there are several thousand validation errors because of missing quotes and tons of other garbage, so if anybody could help me out that would be great!

EDIT:

I gave it a whirl (found some examples), and have something that will work, but would like it to be a little smarter:

Dim input As String = "<tag attribute=value attribute='value' attribute=""value"" />"
Dim test As String = "attribute=(?:(['""])(?<attribute>(?:(?!\1).)*)\1|(?<attribute>\S+))"
Dim result As String = Regex.Replace(input, test, "attribute=""$2""")

This outputs result correctly as:

<tag attribute="value" attribute="value" attribute="value" />

Is there a way I could change (and simplify!) this up a bit so that I could make it look for any attribute name?

UPDATE:

Here’s what I have so far based on the comments. Perhaps it could be improved even more:

Dim input As String = "<tag border=2 style='display: none' width=""100%"" />"
Dim test As String = "\s*=\s*(?:(['""])(?<g1>(?:(?!\1).)*)\1|(?<g1>\S+))"
Dim result As String = Regex.Replace(input, test, "=""$2""")

which produces:

<tag border="2" style="display: none" width="100%" />

Any further suggestions? Otherwise I think I answered my own question, with your help of course.

  • 1 1 Answer
  • 1 View
  • 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-05-12T10:02:35+00:00Added an answer on May 12, 2026 at 10:02 am

    Here is the final product. I hope this helps somebody!

    Imports System.Text.RegularExpressions
    
    Module Module1
    
        Sub Main()
            Dim input As String = "<tag border=2 style='display: none' width=""100%"">Some stuff""""""in between tags==="""" that could be there</tag>" & _
                "<sometag border=2 width=""100%"" /><another that=""is"" completely=""normal"">with some content, of course</another>"
    
            Console.WriteLine(ConvertMarkupAttributeQuoteType(input, "'"))
            Console.ReadKey()
        End Sub
    
        Public Function ConvertMarkupAttributeQuoteType(ByVal html As String, ByVal quoteChar As String) As String
            Dim findTags As String = "</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>"
            Return Regex.Replace(html, findTags, New MatchEvaluator(Function(m) EvaluateTag(m, quoteChar)))
        End Function
    
        Private Function EvaluateTag(ByVal match As Match, ByVal quoteChar As String) As String
            Dim attributes As String = "\s*=\s*(?:(['""])(?<g1>(?:(?!\1).)*)\1|(?<g1>[^>\s]+))"
            Return Regex.Replace(match.Value, attributes, String.Format("={0}$2{0}", quoteChar))
        End Function
    
    End Module
    

    I felt that keeping the tag finder and the attribute fixing regex separate from each other in case I wanted to change how they each work in the future. Thanks for all your input.

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

Sidebar

Related Questions

I came across this post on the MSMobiles.com blog that says the following, among
I have this app written in VB.Net with winforms that shows some stats and
After googling, I have only found only this sad forum question . Is it
I currently have this watermark to print a text on my photos. The sad
I have the following string: $/Mycollection/Branches/Dev/New/php/MySite/src/MySite/somefolder/src/sad.php I need to create regex pattern and take
It's very sad to say that this question was asked several times and never
It's sad how hard it is to find a simple line of code that
Say I have an array like this: [auburn, http://auburn.craigslist.org/web/, http://auburn.craigslist.org/cpg/, http://auburn.craigslist.org/eng/, http://auburn.craigslist.org/sof/, http://auburn.craigslist.org/sad/] What
I'm sad to have to ask this question, but I'm not even sure of
This is the second time that I have this really weird problem with WCF

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.