Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can the StringReplace function be used with wildcards?

Status
Not open for further replies.

CHeighlund

Programmer
Jun 11, 2007
163
US
Can the Delphi StringReplace function be used with wildcard values? That is, if I wanted to, for data entry purposes, read in a file with datestamps in it, and put quotes around the stamps, would the StringReplace function allow me to do so without having to know the exact string value?

Ex: Read in value xx/yy/zzzz. Convert to 'xx/yy/zzzz', where the values of xx, yy, and zzzz remain the same. Again, can StringReplace do this, or do I need to look into other methods?
 
No, but there are always workarounds. In your example you could simply do
Code:
  DateStr := '''' + DateStr + '''';
 
Possibly more elegant:
Code:
DateStr := QuotedStr( DateStr );

Andrew
Hampshire, UK
 
I apologize, I should have made this clearer.

The datestamp isn't the entire string. The values being drawn in are from a csv file, and the (varying) date value is stuck near the center of each record. Because the records before it have variable size, I don't think I can just pluck out the particular substr I need to work with. I apologize for the confusion.

I already know about QuotedStr(), although I thank you for bringing it up. My problem was concerned with the proper method to isolate that portion, although following your suggestion may help me to get around the mono-focus block I'm running into. Thank you.
 
It looks like you need to use regular expressions. As far as I know, the Delphi VCL does not provide support for regular expressions but you can get third party components. Take a look at


Regular expressions provide a very powerful tool for search and replace type operations.

Andrew
Hampshire, UK
 
It looks like you need to use regular expressions.
I beg to differ. It should be rather simple to parse a string for a fixed-length substring with constant delimiters, even though the delimiters are within the substring. It just takes a little arithmetic: minus 2, plus 4.

CHeighlund, don't over-think or over-complicate this.
 
ok harebrain, the OP said:
The values being drawn in are from a csv file, and the (varying) date value is stuck near the center of each record. Because the records before it have variable size, I don't think I can just pluck out the particular substr I need to work with.

Assuming the two following strings are the text coming from the csv and you needed to extract the date portion, how would you do it?

[tt]
This is the beginning of the string 1 234 5 and here is the 1/15/2007 that I am 77895 trying to extract 58 579
This is the second string 5 624 5 and I need the 2/15/2007 now but it's in a different place 58 710[/tt]

(I'm not being obnoxious or anything, I'd really like to know how you would do something like that!)

Leslie

In an open world there's no need for windows and gates
 
Hi Leslie,

The trick is identifying something that is constant across all records. In this case you could say the constant thing will be a slash (/) followed by 2 characters followed by a slash. You could then write some code the searches for a slash in each record, and then checks if the character 3 bytes ahead is also a slash - if it is, you extract a portion around that spot as necessary - that's what harebrain was suggesting.

You could tie it down further of course, by also checking if the characters in between the slashes are 0..9 and also if the 2 prior to the slash are 0..9 and the 4 after the second slash are 0..9. With that sort of rigid checking, you can be sure you've found a date. You could even do a StrToDate check to be sure it's a valid date.

Regular expressions do this sort of thing for you automatically by allowing you to specify a mask such as '00/00/0000' where the 0's represent any number and the slashes are literal. A regex search simply finds any sequence of characters in a string that match the mask. Plus regex searches allow for wildcards so you can have a variable number of characters between literals in the mask.
 
les said:
...here is the 1/15/2007 that I am 77895 trying ...

Note that neither of your examples conforms to the OP's spec. Could be why you're having trouble with this.

But, what Griffyn said: find a slash, back up 2, extract a substring of 10, and see if that's a date. Simple.

REs are wonderful. But, if REs aren't already a familiar tool, the overhead and the learning curve in this case argue against it. Unless the OP has some time on his/her hands, I'd advise a more direct approach using intrinsic string-handling routines.

And when a programmer does have spare time, studying up on REs will pay off better than studying almost anything else.
 
Note that neither of your examples conforms to the OP's spec.
The only specifications I saw are:

The datestamp isn't the entire string. The values being drawn in are from a csv file, and the (varying) date value is stuck near the center of each record. Because the records before it have variable size, I don't think I can just pluck out the particular substr I need to work with. I apologize for the confusion.

I have a date near the center of each record and variable size of information prior to that date....am I missing something?

Where can I find more on Regular Expressions and how to use them?
 
Just found this in Companion Disk 1 to D7 (can't vouch for earlier/later versions.)

On the CD, look for X:\droopy_eyes_software\faststrings -- the FastStringFuncs.pas unit has a function, StringMatches, that performs the kind of wildcard matching that the OP requested.

Still, I think the approach suggested by Griffyn and myself is safer: using wildcards leaves more chances of finding something other than a date. But we did find a wildcard match routine for you. Enjoy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top