comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: String Spliting
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: String Spliting [message #49347] Thu, 20 July 2006 06:37
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
ChristopherFlorio@gmail.com wrote:
> Michael Galloy wrote:
>> ChristopherFlorio@gmail.com wrote:
>>> Actually now I am having a new problem. I want to put text on the other
>>> side of the asterisk.
>>>
>>> file='ImageEarth2.img'
>>> filter='Image*.img'
>>>
>>> file=STRJOIN(STRSPLIT(file,filter,/EXTRACT,/REGEX))
>>> file='ImageEarth2.img'
>>>
>>> with file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))
>>> file='Erth2'
>>>
>>> Any Suggestions?
>>>
>> The desired output is 'ImageEarth2'? I'm not sure what the entire line
>> looks like, but how about this?
>>
>> IDL> file = 'ImageEarth2.img'
>> IDL> s = stregex(file, '([[:alnum:]]+)\.img', /extract, /subexpr)
>> IDL> print, s[1]
>> ImageEarth2
>
> The desired output is "Earth2". I want to eliminate all 'filter'
> information from 'file'.

OK:

IDL> file='ImageEarth2.img'
IDL> s = stregex(file, 'Image([[:alnum:]]+)\.img', /extract, /subexpr)
IDL> print, s[0]
ImageEarth2.img
IDL> print, s[1]
Earth2

Mike
--
www.michaelgalloy.com
Re: String Spliting [message #49349 is a reply to message #49347] Thu, 20 July 2006 04:13 Go to previous message
ChristopherFlorio is currently offline  ChristopherFlorio
Messages: 23
Registered: April 2006
Junior Member
Michael Galloy wrote:
> ChristopherFlorio@gmail.com wrote:
>> Actually now I am having a new problem. I want to put text on the other
>> side of the asterisk.
>>
>> file='ImageEarth2.img'
>> filter='Image*.img'
>>
>> file=STRJOIN(STRSPLIT(file,filter,/EXTRACT,/REGEX))
>> file='ImageEarth2.img'
>>
>> with file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))
>> file='Erth2'
>>
>> Any Suggestions?
>>
>
> The desired output is 'ImageEarth2'? I'm not sure what the entire line
> looks like, but how about this?
>
> IDL> file = 'ImageEarth2.img'
> IDL> s = stregex(file, '([[:alnum:]]+)\.img', /extract, /subexpr)
> IDL> print, s[1]
> ImageEarth2
>
> Mike
> --
> www.michaelgalloy.com

The desired output is "Earth2". I want to eliminate all 'filter'
information from 'file'.
Re: String Spliting [message #49353 is a reply to message #49349] Wed, 19 July 2006 13:03 Go to previous message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
ChristopherFlorio@gmail.com wrote:
> Actually now I am having a new problem. I want to put text on the other
> side of the asterisk.
>
> file='ImageEarth2.img'
> filter='Image*.img'
>
> file=STRJOIN(STRSPLIT(file,filter,/EXTRACT,/REGEX))
> file='ImageEarth2.img'
>
> with file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))
> file='Erth2'
>
> Any Suggestions?
>

The desired output is 'ImageEarth2'? I'm not sure what the entire line
looks like, but how about this?

IDL> file = 'ImageEarth2.img'
IDL> s = stregex(file, '([[:alnum:]]+)\.img', /extract, /subexpr)
IDL> print, s[1]
ImageEarth2

Mike
--
www.michaelgalloy.com
Re: String Spliting [message #49354 is a reply to message #49353] Wed, 19 July 2006 12:49 Go to previous message
mmiller3 is currently offline  mmiller3
Messages: 81
Registered: January 2002
Member
>>>> > ChristopherFlorio <ChristopherFlorio@gmail.com> writes:

> I am having a problem with the STRSPLIT function. I am
> using it to extract the file extension from a string where
> it might not be at the end. However in the process I am
> also eliminating addition elements, not found in the
> extension. For example

> file='Image.img' filter='*.img'

> file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))

> file='Iae'

> desired output: file='Image'

The pattern argument to strsplit is either a single character, or
a regular expression. You are using '*.img' as a regular
expresion and therefore are splitting on any string that looks
like zero or more characters followed by i, m or g. If you want
to split in the '.', use '.' as your split: strsplit(file, '.',
/extract)

IDL> print, (strsplit('Image.img', '.', /extract))[0]
Image

If you want to strip off the extension, you can use strmid and
strpos. (Reverse_search is handy for gettign the last match,
rather than the first)

IDL> file='Image.img'
IDL> print, strmid(file, 0, strpos(file,'.', /reverse_search))
Image
IDL> file='Image.with.multiple.periods.img'
IDL> print, strmid(file, 0, strpos(file,'.'))
Image
IDL> print, strmid(file, 0, strpos(file,'.', /reverse_search))
Image.with.multiple.periods


Mike
Re: String Spliting [message #49356 is a reply to message #49354] Wed, 19 July 2006 12:04 Go to previous message
ChristopherFlorio is currently offline  ChristopherFlorio
Messages: 23
Registered: April 2006
Junior Member
Actually now I am having a new problem. I want to put text on the other
side of the asterisk.

file='ImageEarth2.img'
filter='Image*.img'

file=STRJOIN(STRSPLIT(file,filter,/EXTRACT,/REGEX))
file='ImageEarth2.img'

with file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))
file='Erth2'

Any Suggestions?

JD Smith wrote:
> On Wed, 19 Jul 2006 07:49:10 -0700, ChristopherFlorio wrote:
>
>> I am having a problem with the STRSPLIT function. I am using it to extract
>> the file extension from a string where it might not be at the end.
>> However in the process I am also eliminating addition elements, not found
>> in the extension.
>> For example
>>
>> file='Image.img'
>> filter='*.img'
>>
>> file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))
>>
>> file='Iae'
>>
>> desired output:
>> file='Image'
>>
>> Is there a way, with the same input as above, not to extract individual
>> parts of the name found in the filter/pattern?
>
> Just a guess, but you'd probably be happier with:
>
> file=file_basename(file,'.img')
>
> (which strips any leading directory too).
>
> JD
Re: String Spliting [message #49357 is a reply to message #49356] Wed, 19 July 2006 10:39 Go to previous message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Wed, 19 Jul 2006 07:49:10 -0700, ChristopherFlorio wrote:

> I am having a problem with the STRSPLIT function. I am using it to extract
> the file extension from a string where it might not be at the end.
> However in the process I am also eliminating addition elements, not found
> in the extension.
> For example
>
> file='Image.img'
> filter='*.img'
>
> file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))
>
> file='Iae'
>
> desired output:
> file='Image'
>
> Is there a way, with the same input as above, not to extract individual
> parts of the name found in the filter/pattern?

Just a guess, but you'd probably be happier with:

file=file_basename(file,'.img')

(which strips any leading directory too).

JD
Re: String Spliting [message #49358 is a reply to message #49357] Wed, 19 July 2006 09:51 Go to previous message
ChristopherFlorio is currently offline  ChristopherFlorio
Messages: 23
Registered: April 2006
Junior Member
Thanks. Works fine now.

Michael Galloy wrote:
> ChristopherFlorio@gmail.com wrote:
>> I am having a problem with the STRSPLIT function.
>> I am using it to extract the file extension from a string where it
>> might not be at the end.
>> However in the process I am also eliminating addition elements, not
>> found in the extension.
>> For example
>>
>> file='Image.img'
>> filter='*.img'
>>
>> file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))
>>
>> file='Iae'
>>
>> desired output:
>> file='Image'
>>
>> Is there a way, with the same input as above, not to extract individual
>> parts of the name found in the filter/pattern?
>>
>
> From the docs, about a normal "filter" argument:
>
> "One or more single characters, each of which is considered to be a
> separator. String will be split when any of the characters is detected.
> For example, if Pattern is " ," String will be split whenever either a
> space or a comma is detected. In this case, IDL performs a simple string
> search for the specified characters. This method is simple and fast."
>
> But the filter can also be a regular expression:
>
> IDL> file = 'Image.img'
> IDL> filter = '\.img'
> IDL> print, strjoin(strsplit(file, filter, /regex, /extract))
> Image
>
> The "\" is needed in the filter to escape "."'s special meaning (it
> matches any character).
>
> By the way, the docs on a regular expression "filter" argument:
>
> "If the REGEX keyword is specified, Pattern is considered to be a single
> regular expression (as implemented by the STREGEX function). This method
> is slower and more complex, but can handle extremely complicated Pattern
> strings."
>
> If you want more information about regular expressions, see
>
> http://michaelgalloy.com/2006/06/11/regular-expressions.html
>
> Mike
> --
> www.michaelgalloy.com
Re: String Spliting [message #49361 is a reply to message #49358] Wed, 19 July 2006 08:06 Go to previous message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
ChristopherFlorio@gmail.com wrote:
> I am having a problem with the STRSPLIT function.
> I am using it to extract the file extension from a string where it
> might not be at the end.
> However in the process I am also eliminating addition elements, not
> found in the extension.
> For example
>
> file='Image.img'
> filter='*.img'
>
> file=STRJOIN(STRSPLIT(file,filter,/EXTRACT))
>
> file='Iae'
>
> desired output:
> file='Image'
>
> Is there a way, with the same input as above, not to extract individual
> parts of the name found in the filter/pattern?
>

From the docs, about a normal "filter" argument:

"One or more single characters, each of which is considered to be a
separator. String will be split when any of the characters is detected.
For example, if Pattern is " ," String will be split whenever either a
space or a comma is detected. In this case, IDL performs a simple string
search for the specified characters. This method is simple and fast."

But the filter can also be a regular expression:

IDL> file = 'Image.img'
IDL> filter = '\.img'
IDL> print, strjoin(strsplit(file, filter, /regex, /extract))
Image

The "\" is needed in the filter to escape "."'s special meaning (it
matches any character).

By the way, the docs on a regular expression "filter" argument:

"If the REGEX keyword is specified, Pattern is considered to be a single
regular expression (as implemented by the STREGEX function). This method
is slower and more complex, but can handle extremely complicated Pattern
strings."

If you want more information about regular expressions, see

http://michaelgalloy.com/2006/06/11/regular-expressions.html

Mike
--
www.michaelgalloy.com
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Surface plots in spherical coordinates
Next Topic: Re: mpfitexpr using functargs

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sun Oct 12 19:20:28 PDT 2025

Total time taken to generate the page: 0.72258 seconds