Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Hard Question? String and Replacing

Hi all,

I have a problem with some html code where for example:

It says: class=wrapper  ... when it sould say class="wrapper"

Is the any way to look for class=  and add the " and then after whatever the class name is add the closing " after it?

I need it to search and replace all found in a memo please.

hope you can help

thx

st3vo
Avatar of ST3VO
ST3VO
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Note: It's actually happening to other different codes like width=80 should be width="80" , so could the code add the " after all = and then after what comes next pls?

thx
Avatar of MerijnB
You can use a regular expression for this. I don't the expression by heart, but maybe for now you have enough with the name to get a little further.
You can use this function :

Memo.Text:=AddComma(Memo.Text,'class=');

or combining the two :

Memo.Text:=AddComma(AddComma(Memo.Text,'class='),'width=');

you might even try :
Memo.Text:=AddComma(Memo.Text,'=');

Features : it will not add quotes if they are found just after the StrBefore
Limitations :
 - it will not verify the presence of double "" within the parameters.
- case sensitive search
function AddComma(Str,StrBefore:String;Coma:Char='"';
                  Delim:String=' '):String;
Var
 P,P2:Pos;
begin
 P:=Pos(StrBefore,Str);
 While P>0 do
  begin
   P:=P+Length(StrBefore);
   if Str[P]<>Coma Then
    begin
     P2:=PosEx(Delim,Str,P);
     if P2<P Then P2:=Length(Str)+1;   
     Str:=Copy(Str,1,P-1)+Coma+Copy(Str,P,P2-P)
      +Coma+Copy(Str,P2,Length(Str)-P2+1);
    end Else P2:=PosEx(Coma,Str,P);
   P:=PosEx(StrBefore,Str,P2);
  end;
 Result:=Str;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I read it correctly, it would add " also when it's there already, nor would manage the double quotes as well. It would need something much more complex, does it not ?
RegExpression are powerful, but yes its a very steep leaning curve. And even if you have used them a lot at a moment, just a few months or year later it's still knotting your brain ;o/
Still, I'm taking this RegExpr unit as I never used one in a program, it might prove useful. I have purchased a powerful regexpr tool with scripting that worked well for me when I have complex cases that comes again and again.
Also UltraEdit have a good RegExp engine for everyday use (I Love UE :o)
argh again an old unit with version specific defines :o)
Thank you Borland for such a twisted VER_XXX defining, everybody have done it's own defining system...

I have added this line, RegExpr@L52 , to add quick support for version above D7 (not tested, but should do it)
{$IFNDEF D2} {$DEFINE D8PLUS} {$DEFINE D7} {$DEFINE D6} {$DEFINE D5} {$DEFINE D4} {$DEFINE D3} {$DEFINE D2} {$ENDIF} // Above D7

Open in new window

ok MerjinB, talking about brain knots, do you have a help file somewhere still ? Or even a simple document with the syntax used by this doc ?
I meant syntax used by this component, silly me
> If I read it correctly, it would add " also when it's there already
No it does not

> nor would manage the double quotes as well
What do you mean?
> argh again an old unit with version specific defines :o)
Where do you problems, I use this one in D2006 without problems.

>   ok MerjinB, talking about brain knots, do you have a help file somewhere still ? Or even a simple document with the syntax used by this doc ?                          

This one is nice: http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
> Where do you problems, I use this one in D2006 without problems.
I did not try it, but since it stops short on VER150  to define all D2..D7 I supposed it would disable some functionalities for newer compilers. Maybe it works fine but with less efficiency. I'll check

You should check first :)

It's only some things like default arguments and an overloaded method, not really exciting...
I've tested it in Delphi 2007, with and without the line, and yes it changes (very) few things, like disabling some warnings and do not enabled DefaultParams and Overloading, which can be handy.
That does not stop you from using it, but with a little less functionalities
every free functionality is worth getting ;o)
Can you explain what you mean with:

> nor would manage the double quotes as well

usually, quotes are used when a parameter can take any kind of text with spaces, and quotes that are doubled. I don't use HTML enough to remember if it can be the case there, but that could not hurt. Anyway, that would be a very rare case to have a parameter with quotes that must be doubled without spaces, so I guess this was a theoretical question, and I did not manage it in my simple function. Though, I believe it would be good to extend the regular expression to support of parameters that do have already some quotes around, even if just in case it's run twice on the same file, or at some point the source giving those files correct itself
Avatar of ST3VO

ASKER

Hi all and thanks for your help.

1st: epasquier: I get lots of errors with your function so cannot test it:

[DCC Error] Unit1.pas(158): E2007 Constant or type identifier expected
[DCC Warning] Unit1.pas(161): W1023 Comparing signed and unsigned types - widened both operands
[DCC Error] Unit1.pas(166): E2003 Undeclared identifier: 'PosEx'
[DCC Warning] Unit1.pas(167): W1023 Comparing signed and unsigned types - widened both operands

MerijnB:  I'm going to test yours now but I don't understand how it works, looks quite complex and if I have to create new rules I think I will run into problems... :o/

Avatar of ST3VO

ASKER

Works like a charm! Thanks :o)