armze3
armze3

Reputation: 55

Split a String and store into context variable

I have some dynamic files which will follow one of these patterns:

AAA_BBB_Data_CCC_DDD_<logical area for data split 0-9>_<Seq_Num 0-9>.csv
AAA_BBB_Data_CCC_DDD_<logical area for data split 0-9>_<Seq_Num 0-9>_manifest.csv
AAA_BBB_Data_CCC_DDD_<logical area for data split 0-9>_<Seq_Num 0-9>_manifestSummary.csv

I want to get the logical area of split and store it in a context variable splitPrt and I want to get seq_num and store it in context variable SeqNum

The filenames are stored in the global Variable

((String)globalMap.get("tFileList_3_CURRENT_FILE"))

I want to avoid using tJava if possible, but if not I can use it.

Upvotes: -1

Views: 91

Answers (1)

DuesserBaest
DuesserBaest

Reputation: 2829

I hesitantly add this as an answer as requested in the comments. I am not a Java Expert and will not handle code for the exception handeling to return 99 in case the regex matches, although I hope it would be rather straight forward.

Try the following regex and look here as to how you would extract fields using it.

^AAA_BBB_Data_CCC_DDD_(?<SplitStr>\d)_(?<SeqNr>\d)(?:_manifest(?:Summary)?)?\.csv$

See: regex101


Explanation

  • ^AAA_BBB_Data_CCC_DDD_: Match a string starting with literal AAA_BBB_Data_CCC_DDD_
  • (?<SplitStr>\d): then capture the following digit to the group "SplitStr"
  • _: followed by literal _
  • (?<SeqNr>\d): then capture the following digit to the group "SeqNr"
  • (?:_manifest(?:Summary)?)?: and optionally _manifest or _manifestSummary
  • \.csv$: before literal .csv at the end of the file.

Upvotes: 0

Related Questions