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

Matching and replacing HTML Tag Attribute Values

Last post 07-06-2008, 7:54 PM by ddrudik. 3 replies.
Sort Posts: Previous Next
  •  07-06-2008, 12:45 AM 43787

    Matching and replacing HTML Tag Attribute Values

    Hello there,

    I'm very inexperienced when it comes to Regular Expressions, and as fortune would have it, have been given a sophisticated RegExp problem that I need to solve. In all honesty, I'm not even sure if this requirement is possible using a Regular Expression, but here it is anyway.

    By the way, as far as I know, we're using Perl RegExp syntax.

    If our application receives HTML containing a font tag with a size attribute, we need to ensure this attribute is not assigned a value greater than 3.

    For example, if the font tag consists of the following:

    <font face="arial" size="250">

    It needs to be replaced with:

    <font face="arial" size="3">

    I'm not even sure if I can apply greater-than or less-than ranges within a Regular Expression. Can this be done?

    I've managed to construct the following RegExp to identify a well-formed font tag, but this is about as far as I can get:

    <font(\s*[a-z]*=?"?#?[a-z]*[0-9]*"?\s*)*>


    Am I trying to accomplish the impossible?

    Cheers.

    James
  •  07-06-2008, 8:54 AM 43791 in reply to 43787

    Re: Matching and replacing HTML Tag Attribute Values

    Raw Match Pattern:
    (<font\b[^>]*size=)(['"]?)(?![123]\b)\d+\2([^>]*>)

    Raw Replace Pattern:
    $1"3"$3

    Note that you can have a class= attribute on <font> tags that will cause this process to fail to recognize font sizes properly.

    http://www.myregextester.com/?r=232


  •  07-06-2008, 7:38 PM 43799 in reply to 43791

    Re: Matching and replacing HTML Tag Attribute Values

    Hey, that worked really well. Thanks a lot. Big Smile

    I knew Regular Expressions were powerful, but I didn't realise you could do that much with them!

  •  07-06-2008, 7:54 PM 43801 in reply to 43799

    Re: Matching and replacing HTML Tag Attribute Values

    Thanks, happy to help.
View as RSS news feed in XML