stregex with multiple matches [message #88670] |
Thu, 29 May 2014 11:31  |
PMan
Messages: 61 Registered: January 2011
|
Member |
|
|
Hello,
Can stregex match an expression multiple times? It seems not, but I get the feeling there ought to be a way to do so. For example:
IDL> time_regex = '([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])'
IDL> timeStr = '10:21:41"
IDL> print, stregex(timeStr, time_regex)
0
Ok, that is what expected
IDL> timeStr = '10:21:41 - 11:32:00"
IDL> print, stregex(timeStr, time_regex)
0
But in the second example, why aren't there two matches? can I change my regex in some why to get multiple matches?
Thanks
|
|
|
Re: stregex with multiple matches [message #88671 is a reply to message #88670] |
Thu, 29 May 2014 11:41   |
PMan
Messages: 61 Registered: January 2011
|
Member |
|
|
On Thursday, May 29, 2014 2:31:11 PM UTC-4, Paul Mallas wrote:
> Hello,
>
>
>
> Can stregex match an expression multiple times? It seems not, but I get the feeling there ought to be a way to do so. For example:
>
>
>
>
>
> IDL> time_regex = '([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])'
>
>
>
>
>
> IDL> timeStr = '10:21:41"
>
> IDL> print, stregex(timeStr, time_regex)
>
> 0
>
>
>
> Ok, that is what expected
>
>
>
>
>
> IDL> timeStr = '10:21:41 - 11:32:00"
>
> IDL> print, stregex(timeStr, time_regex)
>
> 0
>
>
>
> But in the second example, why aren't there two matches? can I change my regex in some why to get multiple matches?
>
>
>
> Thanks
I just noticed the quote/double quote mis-match, but the result is the some
|
|
|
Re: stregex with multiple matches [message #88672 is a reply to message #88670] |
Thu, 29 May 2014 11:42   |
PMan
Messages: 61 Registered: January 2011
|
Member |
|
|
On Thursday, May 29, 2014 2:31:11 PM UTC-4, Paul Mallas wrote:
> Hello,
>
>
>
> Can stregex match an expression multiple times? It seems not, but I get the feeling there ought to be a way to do so. For example:
>
>
>
>
>
> IDL> time_regex = '([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])'
>
>
>
>
>
> IDL> timeStr = '10:21:41"
>
> IDL> print, stregex(timeStr, time_regex)
>
> 0
>
>
>
> Ok, that is what expected
>
>
>
>
>
> IDL> timeStr = '10:21:41 - 11:32:00"
>
> IDL> print, stregex(timeStr, time_regex)
>
> 0
>
>
>
> But in the second example, why aren't there two matches? can I change my regex in some why to get multiple matches?
>
>
>
> Thanks
I just noticed my quote/double quote mis-match, but the result is the same.
|
|
|
Re: stregex with multiple matches [message #88673 is a reply to message #88672] |
Thu, 29 May 2014 16:59  |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
>> Can stregex match an expression multiple times?
Not that I am aware of. You have to repeat the search string twice (note I added "[^0-9]*" to the end of the regex).
nRepeats = 2
time_regex1 = '([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])[^0-9]*'
time_regex = strjoin(replicate(time_regex1, nRepeats))
timeStr = '10:21:41 - 11:32:00'
print, stregex(timeStr, time_regex, /SUBEXP, /EXTRACT)
-------------------
If you have no way of knowing how many times you will have, you can make the whole expression optional.
nRepeats = 5
time_regex1 = '(([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])[^0-9]*)?'
time_regex = strjoin(replicate(time_regex1, nRepeats))
timeStr = '10:21:41 - 11:32:00'
result = stregex(timeStr, time_regex, /SUBEXP, /EXTRACT)
for i = 0, 12 do print, result[i]
10:21:41 - 11:32:00
10:21:41 -
10
21
41
11:32:00
11
32
00
|
|
|