SOLVED

Issue splitting string with regex in velocity

Go to solution
Victor_Herrero
Level 5

Issue splitting string with regex in velocity

Hi! 

I have been having issues trying to split a series of strings in one line by different delimiters. 

This is the code I am using: 

 

#set ($latestMeetingTimeFromArray = $latestMeetingTimeFrom.split("\s|[:\.]"))
#set ($latestMeetingTimeFromArrayString = $convert.toString($latestMeetingTimeFromArray))
array : $latestMeetingTimeFromArrayString <br>

 

(I'm printing out the array for debugging)

$latestMeetingTimeFrom can have values like the following: 

 

12:00 PM
1:30 AM
11.00 PM

 

My aim is to store hours minutes and the meridiem in three separate variables, and as you can see, the delimiters are colon, dot and space. 

I have tried all sorts of supposedly valid regex expressions to indicate all possible delimiters (with character set, three individual chars in OR, one char set with all three... ) and I have even tried passing a limiter of 0 and 3, but still the array ends up containing only one character: the hours value. 

Example output:

Victor_Herrero_0-1615203456631.png

The only way I have managed to get this to work is by replacing all delimiters with the same one (i.e. a space), then simplyfying the regex to just that one character. 

Additionally, those three lines of code already break the email when there is a "." separating hours and minutes. Here is the error

Victor_Herrero_0-1615205263580.png

 

Any idea why this is happening? 

Tags (3)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Issue splitting string with regex in velocity

I don't understand why you'd be converting the Array back into a String, if you want individual variables?

 

In any case you should be using the built-in DateTime functions, as this is a DateTime parsing exercise, not a proprietary format:

#set( $meetingDT = $convert.toCalendar($convert.parseDate($latestMeetingTimeFrom,"h:mm a") ))
#set( $hour = $date.format( $meetingDT,"h") )
#set( $minute = $date.format( $meetingDT,"mm") )
#set( $meridiem = $date.format( $meetingDT,"a") )

 

View solution in original post

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Re: Issue splitting string with regex in velocity

I don't understand why you'd be converting the Array back into a String, if you want individual variables?

 

In any case you should be using the built-in DateTime functions, as this is a DateTime parsing exercise, not a proprietary format:

#set( $meetingDT = $convert.toCalendar($convert.parseDate($latestMeetingTimeFrom,"h:mm a") ))
#set( $hour = $date.format( $meetingDT,"h") )
#set( $minute = $date.format( $meetingDT,"mm") )
#set( $meridiem = $date.format( $meetingDT,"a") )

 

Victor_Herrero
Level 5

Re: Issue splitting string with regex in velocity

I was converting the array back into a string only for debugging. I wanted to see if it was forming correctly. 

Your suggestion to use built-in time functions is definitely more elegant than me attempting to re-invent the wheel and I have been able to successfully implement it to create an "Add to Google" link. 

 

That aside, a problem we have is with data being incorrect. There are some records on database that display "." instead of ":" to separate hours from minutes and I still have had replace those. 

 

What I was trying to do with the split method was to avoid chaining replace methods by leveraging one regular expression. 

Split seems to expect a regular expression instead of a single character, so I wanted to bundle all possible wrong characters into on eexpression. But I have not been able to split by several delimiters, even if my regular expression seemingly is able to correctly identify all delimiters I needed (".",":" and " "). When attempting to split by it, the resulting array only contained the first element. 

 

I may be wrong but it seems like you can't use several delimiters in one expression to split once (so basically use "|" inside your expression). 

SanfordWhiteman
Level 10 - Community Moderator

Re: Issue splitting string with regex in velocity


I may be wrong but it seems like you can't use several delimiters in one expression to split once (so basically use "|" inside your expression). 


You sure can:

#set( $latestMeetingTimeFrom = "a;b:c.d" )
#set( $latestMeetingTimeFromArray = $latestMeetingTimeFrom.split("[;:.]"))
#foreach( $itm in $latestMeetingTimeFromArray )
${itm}
#end
Victor_Herrero
Level 5

Re: Issue splitting string with regex in velocity

Thank you Sanford!

I swear I tried a character set and it still didn't pick up more than the first item. 

Maybe I was doing other things wrong at the same time. I certainly was escaping the "." (since it means "any character except line break") which doesn't seem to be necessary? 

That is confusing. 

SanfordWhiteman
Level 10 - Community Moderator

Re: Issue splitting string with regex in velocity


That aside, a problem we have is with data being incorrect. There are some records on database that display "." instead of ":" to separate hours from minutes and I still have had replace those.

Yikes, kind of disturbing to have different formats for datetimes.

 

But I would still try to parse it using multiple date time formats — if the parsed result is null from one format, try the next — as opposed to a RegEx replace, if the formats are all valid SimpleDateFormats.

Victor_Herrero
Level 5

Re: Issue splitting string with regex in velocity

I can try to see if the date tool is able to recognize the "." as a delimiter. Yes it's scary to have this problem and I think we will be able to fix it, even retroactively, but until then we will need to work around it. 

SanfordWhiteman
Level 10 - Community Moderator

Re: Issue splitting string with regex in velocity


I can try to see if the date tool is able to recognize the "." as a delimiter.

The period character has no special meaning in SimpleDateFormats, so it certainly will work. These are both valid formats, provided the input data matches:

HH:mm:ss
HH.mm.ss

 

Victor_Herrero
Level 5

Re: Issue splitting string with regex in velocity

I will certainly try that then, since I think our email script is not the right place to fix this problem. We should certainly fix at the source, and I can leave that replacement out of the code, it will be cleaner and clearer. 

 

Thank you!