Re: matcher is failing

"Spencer Allain via users" <users-GSC0n/0aZo1d/SJB6HiN2Ni2O/[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <[email protected]>
 You have changed the first grouping into a non-capture group with ?:, so matcher[0][3] will be null, and matcher[0][1] will be the month and matcher[0][2] will be the year.
I also believe that you should be able to reduce [\\\-\\\/] to be simply [-\/] because of using slashy-string for the regex (as only forward slash needs to be escaped)
-Spencer
    On Sunday, July 9, 2023 at 12:08:17 PM EDT, James McMahon <[email protected]> wrote:  
 
 Correction: this is my code...
else if ( candidate =~ /^(?:0?[1-9]|[12]\d|3[01])[\\\-\\\/]+(\d{2}|\d{1})[\\\-\\\/]+(\d{4}|\d{2})$/ ) {        

              log.error("BINGO!")

              matcher = candidate =~ /^(?:0?[1-9]|[12]\d|3[01])[\\\-\\\/]+(\d{2}|\d{1})[\\\-\\\/]+(\d{4}|\d{2})$/
              matchedSubstring = matcher[0][0]
              log.error("Matcher: ${matcher.toString()}")
              log.error("Matched substring: ${matchedSubstring}")
              day = matchedSubstring[matcher[0][1]]
              month = matchedSubstring[matcher[0][2]]
              year = matcherSubstriong[matcher[0][3]]

              log.error("Day: ${day}")
              log.error("Month: ${month}")
              log.error("Year: ${year}")

              log.error("Length of Day: ${day.length()}")
              log.error("Length of Month: ${month.length()}")
              log.error("Length of Year: ${year.length()}")

           }

I suspect I need to look at how I'm setting day, month, and year from matcher.
The log output:2023-07-09 16:04:47,406 ERROR [Timer-Driven Process Thread-10] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Candidate: 06-14-2023
2023-07-09 16:04:47,406 ERROR [Timer-Driven Process Thread-10] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] BINGO!
2023-07-09 16:04:47,406 ERROR [Timer-Driven Process Thread-10] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Matcher: java.util.regex.Matcher[pattern=^(?:0?[1-9]|[12]\d|3[01])[\\\-\\/]+(\d{2}|\d{1})[\\\-\\/]+(\d{4}|\d{2})$ region=0,10 lastmatch=06-14-2023]
2023-07-09 16:04:47,406 ERROR [Timer-Driven Process Thread-10] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Matched substring: 06-14-2023
2023-07-09 16:04:47,406 ERROR [Timer-Driven Process Thread-10] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Could not parse: 06-14-2023

On Sun, Jul 9, 2023 at 11:59 AM James McMahon <[email protected]> wrote:

Hello. I have a conditional clause in my Groovy script that attempts to parse a date pattern of this form: 06-14-2023. It fails - I believe in the matcher.
I am running from a NiFi ExecuteScript processor. Here is my conditional:
              } else if ( candidate =~ /^(?:0?[1-9]|[12]\d|3[01])[\\\-\\\/]+(\d{2}|\d{1})[\\\-\\\/]+(\d{4}|\d{2})$/ ) {        

              log.error("BINGO!")

              matcher = candidate =~ /^(?:0?[1-9]|[12]\d|3[01])[\\\-\\\/]+(\d{2}|\d{1})[\\\-\\\/]+(\d{4}|\d{2})$/
              log.error("Matcher: ${matcher.toString()}")
              log.error("Matched substring: ${matchedSubstring}")
              day = matchedSubstring[matcher[0][1]]
              month = matchedSubstring[matcher[0][2]]
              year = matcherSubstriong[matcher[0][3]]

              log.error("Day: ${day}")
              log.error("Month: ${month}")
              log.error("Year: ${year}")

              log.error("Length of Day: ${day.length()}")
              log.error("Length of Month: ${month.length()}")
              log.error("Length of Year: ${year.length()}")

           }

My log output tells me I make it into the conditional, but then I fail on the matcher:
2023-07-09 15:52:23,547 ERROR [Timer-Driven Process Thread-2] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Candidate: 06-14-2023
2023-07-09 15:52:23,547 ERROR [Timer-Driven Process Thread-2] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] BINGO!
2023-07-09 15:52:23,547 ERROR [Timer-Driven Process Thread-2] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Matcher: java.util.regex.Matcher[pattern=^(?:0?[1-9]|[12]\d|3[01])[\\\-\\/]+(\d{2}|\d{1})[\\\-\\/]+(\d{4}|\d{2})$ region=0,10 lastmatch=]
2023-07-09 15:52:23,547 ERROR [Timer-Driven Process Thread-2] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Matched substring: null
2023-07-09 15:52:23,547 ERROR [Timer-Driven Process Thread-2] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Could not parse: 06-14-2023

Can anyone help me get this matcher to work?
Thanks in advance for any help.
Jim
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.