Skip to main content
Solved

Regular Expression to extract the earliest year from Attribute

  • November 9, 2017
  • 6 replies
  • 30 views

I have the following string in an attribute: "Jan 1 1960; Jan 1 1900; Jan 1 1960; 01/05/1890; Jan 1 1960; Jan 1 1960; Jan 1 1960; Jan 1 1960; Jan 1 1960; Jan 1 1960; " I need to extract the earliest year from the attribute, since this is input from a reader the attribute will have different values, but the year will always be in a 4 digit format. I want to use a regular expression to extract the earliest year, in this case it would be 1890. Is this possible?

Best answer by david_r

Here's one way:

  • StringSearcher with a regex like "\\d{4}" to extract all occurrences of 4 consecutive digits. Specify a list name for all the "All matches list name"
  • ListSorter in ascending numerical order on the match list
  • ListIndexer on item 0 to extract the lowest number

Example:

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

6 replies

david_r
Celebrity
  • 8394 replies
  • Best Answer
  • November 9, 2017

Here's one way:

  • StringSearcher with a regex like "\\d{4}" to extract all occurrences of 4 consecutive digits. Specify a list name for all the "All matches list name"
  • ListSorter in ascending numerical order on the match list
  • ListIndexer on item 0 to extract the lowest number

Example:


  • Author
  • 16 replies
  • November 9, 2017

Here's one way:

  • StringSearcher with a regex like "\\d{4}" to extract all occurrences of 4 consecutive digits. Specify a list name for all the "All matches list name"
  • ListSorter in ascending numerical order on the match list
  • ListIndexer on item 0 to extract the lowest number

Example:

Thanks for your answer David. I'm Using FME 2016, the ListSorter transformer doesn't have the defined list in StringSearcher available.

 

 


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3434 replies
  • November 9, 2017
Thanks for your answer David. I'm Using FME 2016, the ListSorter transformer doesn't have the defined list in StringSearcher available.

 

 

It should be there if you expand the Advanced part

 

 


  • Author
  • 16 replies
  • November 9, 2017

Here's one way:

  • StringSearcher with a regex like "\\d{4}" to extract all occurrences of 4 consecutive digits. Specify a list name for all the "All matches list name"
  • ListSorter in ascending numerical order on the match list
  • ListIndexer on item 0 to extract the lowest number

Example:

In FME 2016 I've used the Subexpression Matches List Name which makes the list available to the ListSorter

 

 


david_r
Celebrity
  • 8394 replies
  • November 9, 2017
In FME 2016 I've used the Subexpression Matches List Name which makes the list available to the ListSorter

 

 

FME 2016 and 2017 are identical on this point, you need to specify the "All matches list name", not the "Subexpression matches list name" (there are no sub-expressions in this regex).

takashi
Celebrity
  • 7843 replies
  • November 9, 2017

Just for fun. Sometimes FME can solve a puzzle with a collaborative work of string/math operations and geometric operations.

0684Q00000ArL2gQAF.png

Set OGC Well Known Text to the Geometry Encoding parameter, and set this expression to the Geometry Source parameter in the GeometryReplacer

LINESTRING(@Trim(@ReplaceRegEx(;@Value(text),";.+?(?=\d{4})",",0 "),",; "))