Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Comparing Look alike strings(Date)

hi,
the problem is, I have to extract date from a file. Tell me how can compare string to get only the date out of it. In SQL we have Like() function but I do not know how in c++ we can compare look alike but not exact strings.
As you know strcmp() would not be useful for this purpose.
If anybody can come with a better logic please.
Waiting.
Thanking you all
nadz
[390 byte] By [nadz] at [2007-11-11 8:37:01]
# 1 Re: Comparing Look alike strings(Date)
The string class provided with the STL includes over-ridden boolean operators which permit direct comparison of strings. So you can state if( a < b ) and your program knows how to evaluate this.

Take a look at http://www.cppreference.com/cppstring/string_operators.html
nspils at 2007-11-11 21:01:17 >
# 2 Re: Comparing Look alike strings(Date)
Thanks nsplis,
but see the problem is i need to extract DATE from a large file.
The date could be any date. One solution is that I make a string
and in a loop make it change for day,month,year and compare with every string or potential date strings of file. But you see it does not seems to be a very good logic. Can anybody state any other solution please.
nadz at 2007-11-11 21:02:13 >
# 3 Re: Comparing Look alike strings(Date)
For dates specifically look at time_t and timespan_t and the associated functions. You can put dates into them and get the difference in days for example, or seconds, or whatever.
jonnin at 2007-11-11 21:03:11 >
# 4 Re: Comparing Look alike strings(Date)
Hi,
looks to me like you want some regular expression matching. The boost library has some templates that allow regex matching, managed code in VS.Net allows you also (I belive) to use the regex package. If that's not sufficient - there are lex/yacc parser generators out there free for download.
The idea is to find strings that have a certain format, sth like (in "lex")
[0-3][0-9]"/"[0-1][0-9]"/"[1-2][0-9][0-9][0-9]
could match valid date strings like "01/01/2001", "31/12/1976", etc
unfortunately it could also match invalid date strings like
"00/00/1000" or "30/02/2001".
But if you at least get the initial format right, the validation of the strings ist just a little exercise.

Cheers,

D
drkybelk at 2007-11-11 21:04:11 >
# 5 Re: Comparing Look alike strings(Date)
cheers mate,
let me try this.
nadz at 2007-11-11 21:05:18 >
# 6 Re: Comparing Look alike strings(Date)
Do you know the exact format of the string within the file? In that case you can use regular expressions as said before. If you also know the range of date, i.e., April 2006, but not the day, you can simply use strstr() to locate the substring "2006" within the larger string (i.e. the entire file).
Danny at 2007-11-11 21:06:17 >