0

A regular expression is used to parse text that include '=' and put split string into a stringlist like key value pair.

But if value contains '=', it can not use list.add(key3+'='+'number=10');

key1 this's done
key2 that costs 10 dollars
key3 number=10 // invalid data, error prompt.
...

how to solve? Thank you.

Edit:

Thank you all for help.

If I have to add a string that includes '=' into key, how can I solve it?

for example, the text to be parsed may be like this:

maleConsumer=john 1
maleConsumer=eric 2
femaleConsumer=mary 2
maleConsumer=john 8
...

I use regex reg='\b\S+\b' parse text and to put maleconsumer=john into key of stringlist, so that in stringlist, john's record will be:

maleConsumer=john 9 // maleconsumer=john is key, 9 is value

In such case, how can I do it?

Thank you all for your help again.

2
  • 3
    can you rephrase the question? what exactly are you trying to achieve? for ex. do you want to get key3 10 instead? Commented Jan 12, 2011 at 13:11
  • Check your tags - is this a java / c# / delphi question? Smells like a regex question to me... Commented Jan 12, 2011 at 16:36

6 Answers 6

6

This works fine in Delphi

var
    sl: TStringList;
begin
    sl := TStringList.Create;
    try
        sl.Add('key1=this''s done');
        sl.Add('key2=that costs 10 dollars');
        sl.Add('key3=number=10');
        ShowMessage(sl.Values['key3']); // Displays number=10
    finally
        sl.Free;
    end;
end;

This is better and still works

var
    sl: TStringList;
begin
    sl := TStringList.Create;
    try
        sl.Values['key1'] := 'this''s done';
        sl.Values['key2'] := 'that costs 10 dollars';
        sl.Values['key3'] := 'number=10';
        ShowMessage(sl.Values['key3']); // Displays number=10
    finally
        sl.Free;
    end;
end;

BTW, you can specify the separator with TStringList.NameValueSeparator

Using NameValueSeparator to allow = in key

var
    sl: TStringList;
begin
    sl := TStringList.Create;
    try
        // Select a separater you are sure will never be used
        sl.NameValueSeparator := '|';

        sl.Values['maleConsumer=john'] := '1';
        sl.Values['maleConsumer=eric'] := '2';
        sl.Values['femaleConsumer=mary'] := '2';
        sl.Values['maleConsumer=john'] := '8';

        ShowMessage(sl.Values['maleConsumer=john']); // Displays 8
    finally
        sl.Free;
    end;
end;
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Mikael for your answer. If I change format of input like my edition above, how can i solve the problem? Thank you again.
Added sample with NameValueSeparater to allow = in key
Shame on me... So TStringList can work with different delimiters after all. My ignorance couldn't have shown off better. I stand corrected and my so called 'answer', deleted.
@Garry - Since delphi now supports unicode you should be able to find a character that is extremely unlikely to be a part of the key.
2

Check if the value contains '=':

if(value.indexOf('=') != -1){
 //error prompt
}

Comments

1

Java:

you can use: String.contains() method.

Comments

1

For Delphi; you can set Delimiter and QuoteChar for your strings. Example:

  cars := TStringList.Create;
  // Now add some cars to our list - using the DelimitedText property
  // with overriden control variables
  cars.Delimiter := ' ';        // Each list item will be blank separated
  cars.QuoteChar := '|';        // And each item will be quoted with |'s
  cars.DelimitedText := '|Honda Jazz| |Ford Mondeo| |Jaguar "E-type"|';

look here for detail.

1 Comment

Thank you SimaWB, Please look at my above edition, can I solve the problem?
1

Delphi:

var
  LStringList: TStringList;
  LStrValue: string;
begin
  LStringList := TStringList.Create;
  try
    // set the value of a key
    LStringList.Values['a key'] := 'a value';
    // get the value of a key
    LStrValue := LStringList.Values['a key'];
  finally
    FreeAndNil(LStringList);
  end;// trye
end;

1 Comment

Thank you Dorin, Please look at my above edition, can I solve that again.
1

If you are using Delphi 2009 or later, use TDictionary instead of TStringList. That way you avoid all these hacks required to get TStringList to work properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.