How to crop date from string

I have lots of string in the following format:
E:\PFX\26-Mar-2011\02_01_17.35.54.000.pfx

I need to go over each string and take only its date, it also should be in the following format: 03/26/2011

I can use only std::string

How can I do that?

Thanks
closed account (zb0S216C)
This is clearly a question for homework. Here's your solution:

Don't ask for answers for homework. Your answer will always be no.

Have you even attempted? If so, post your code, and we'll help your from there.

Wazzak
What do you know exactly about that format? Is it always going to start with "E:\PFX\" and have the date immediately after that. Will the date always be in format dd-mon-yyyy ?
Look into string's find and substr methods.
Thanks you for your answer hamsterman
Sorry for my ignorance, I am a .NET developer and C++ is not my strongest side.

E:\PFX is always the same folder, the date and the file names are the only one that changes,
I need to take the date from that full path name

The whole string is type PTR<os_path>
This is a typical job for regular expressions, although I agree: It can also be done without much hassle using the good old find functions.

See http://msdn.microsoft.com/en-us/library/bb982727.aspx for regular expressions in VS2010. There is an installable package for VS2008 too.
Unfortunately i am working on a very old code which is compiled with VS6

my string is type of os_path which was created by ObjectSpace

is there any documentation for that? as far as i know the company was bought or does not exist any more.....



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// os_path header.
// Copyright (c) 1994,1995,1996 by ObjectSpace, Inc.  All rights reserved.
// Email: sales@objectspace.com, ccs.support@objectspace.com
// Last Modified: $Date: 9/10/01 5:06p $

#ifndef OS_FILE_PATH_H
#define OS_FILE_PATH_H

#include <ospace/config.h>


// SYNOPSIS:
// A file system path.

// DESCRIPTION:
// The class `os_path` allows file system paths to be manipulated.
// Conversions allow `os_paths` and `const char*`s to be used interchangeably.


#include <ospace/std/iosfwd>
#include <ospace/std/string>

class os_bstream;


class OS_PUBLIC os_path
  {
  private:
    // This member is first to work-around a bug in the Sun 4.0.1 compiler.
    os_string data_;
    char path_separator_;
    char drive_separator_;
    char extension_separator_;

    friend void os_write( os_bstream&, const os_path& );
    friend void os_read( os_bstream&, os_path& );

  public:
	// Destructor
	virtual ~os_path () {};

    // Constructors.
    inline os_path();
    os_path( const os_string& filepath );
    os_path( const os_string& dirpath, const os_string& filename );
    os_path
      (
      const os_string& dirpath,
      const os_string& base,
      const os_string& extension
      );
    os_path
      (
      char drive,
      const os_string& dirpath,
      const os_string& filename
      );
    os_path
      (
      char drive,
      const os_string& dirpath,
      const os_string& base,
      const os_string& extension
      );
    os_path
      (
      const os_string& servername,
      const os_string& sharename,
      const os_string& dirpath,
      const os_string& filename
      );
    os_path
      (
      const os_string& servername,
      const os_string& sharename,
      const os_string& dirpath,
      const os_string& base,
      const os_string& extension
      );
    os_path( const os_path& path );

    // Comparison
    bool operator==( const os_path& path ) const;
    bool operator==( const os_string& string ) const;
    bool operator!=( const os_path& path ) const;
    bool operator!=( const os_string& string ) const;
    bool operator<( const os_path& path ) const;
    bool operator<( const os_string& string ) const;

    // Assignment.
    os_path& operator=( const os_string& filepath );
    os_path& operator=( const os_path& path );

    // Conversion.
    operator const char*() const;
    operator const os_string&() const;

    // Testing.
    bool absolute() const;
    bool relative() const;
    bool file_path() const;
    bool directory_path() const;
    bool has_directory() const;
    bool has_extension() const;
    bool has_drive() const;
    bool has_unc() const;

    // Components.
    int levels() const;
    bool legal( int index ) const;
    os_string operator[]( int index ) const;

    // Accessors.
    os_string directory() const;
    os_string filename() const;
    os_string head() const;
    os_string tail() const;
    os_string base() const;
    os_string extension() const;
    os_string drive() const;
    os_string unc() const;
    os_string server() const;
    os_string share() const;
    char path_separator() const;
    char drive_separator() const;
    char extension_separator() const;

    // Modifiers.
    os_path& directory( os_string string );
    os_path& head( const os_string& string );
    os_path& tail( const os_string& string );
    os_path& section( int index, const os_string& string );
    os_path& filename( const os_string& string );
    os_path& base( const os_string& string );
    os_path& extension( const os_string& string );
    os_path& unc( const os_string& server, const os_string& share );
    os_path& server( const os_string& string );
    os_path& share( const os_string& string );
    os_path& drive( char drive );
    os_path& no_extension();
    os_path& no_drive();
    os_path& no_unc();
    os_path& cd( os_string dirpath );

    os_path& path_separator( char separator );
    os_path& drive_separator( char separator );
    os_path& extension_separator( char separator );


    // cd.
    os_path cd( os_string dirpath ) const;

    // Simplifying.
    os_path simplified() const;
    void become_simplified();

    // Relative conversion.
    os_path relative_to( const os_string& dirpath ) const;
    void become_relative_to( const os_string& dirpath );

    // Special name testing.
    static bool is_dot_directory( const os_string& name );

    // Printing.
    void print( os_ostream& stream ) const;

    // Static accessors.
    static char default_path_separator();

  protected:
    // Helpers.
    static void validate_drive_letter( char drive );
    os_string make_unc
      (
      const os_string& server,
      const os_string& share
      ) const;
    void proper_path_form(
      os_string&,
      bool ensure_leading_separator = false
      ) const;
    size_t locate_dir_sep() const;

    // Directory section access.
    os_string section( int index, bool absolute = false ) const;
    int absolute_levels() const;
    size_t find_directory( size_t& length ) const;
    size_t find_section( int index, size_t& length ) const;
    void init_separators();

    // Common finds.
    size_t last_extension_separator() const;
  };


#include <ospace/file/path.ipp>
#endif 


Thanks!
This is way too trivial for regex..
If the length of each part of the date is constant, day = my_string.substr(7, 2); month_name = my_string.substr(10, 3); year = my_string.substr(14, 4);
You'll have some issues with converting month name to number. You could have a map<string, string> which stores "Jan" as the key and "01" as the value.
Then result = month + "/" + day + "/" + year;
Topic archived. No new replies allowed.