[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Hard Question? String and Replacing

Posted on 2009-12-17
18
Medium Priority
?
491 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:ST3VO
  • 9
  • 6
  • 3
18 Comments
 

Author Comment

by:ST3VO
ID: 26076878
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
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 26078100
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.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26078575
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 19

Accepted Solution

by:
MerijnB earned 2000 total points
ID: 26078706
Hi ST3VO,

See a demo using a regular expression. The big advantage is that this should work for everything you throw at it.

The homepage of the regex unit I normally use isn't up anymore, so I've attached it since it's free.

Regular expressions have a somewhat steep learning curve, but it's most certainly worth the effort you put in it.

For the demo I only have a form with a memo on it. The memo contains:
wrapper=class
width=80

The following code makes the replacements you need:

procedure TForm2.FormCreate(Sender: TObject);
var r: TRegExpr;
begin
 r := TRegExpr.Create();

 try
  r.Expression := '(\w+\s*)=(\s*)(\w+)';
  memo1.text := r.Replace(memo1.text, '$1=$2"$3"', true);
 finally
  r.Free();
 end;
end;

Open in new window

RegExpr.pas
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26078743
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)
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26078785
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

0
 
LVL 25

Expert Comment

by:epasquier
ID: 26078807
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 ?
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26078811
I meant syntax used by this component, silly me
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 26078821
> 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?
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 26078839
> 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/
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26078915
> 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

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 26078925
You should check first :)

It's only some things like default arguments and an overloaded method, not really exciting...
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26078975
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
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26078982
every free functionality is worth getting ;o)
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 26079061
Can you explain what you mean with:

> nor would manage the double quotes as well

0
 
LVL 25

Expert Comment

by:epasquier
ID: 26079090
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
0
 

Author Comment

by:ST3VO
ID: 26088108
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/

0
 

Author Closing Comment

by:ST3VO
ID: 31667607
Works like a charm! Thanks :o)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question