Skip to main content
Solved

Regular expression in combination with else/if construction

  • January 11, 2020
  • 13 replies
  • 25 views

lambertus
Enthusiast
Forum|alt.badge.img+23

Hi guys,

I have a table with an attribute 'Ingebruikname'.

This field contains a year, month and day and info in hours. For example: 2019-01-11T13:05:00.000. I created a regular expression to select only the part with information about year, month and day. Like:

2019-01-11T13:05:00.000

I would like to get my attribute 'Ingebruikname' updated to records with only date infromation as shown in bolt. Records with no data for 'Ingebruikname' should be included as well and left empty.

Do you have any ideas how to solve?

 

 

Best answer by takashi

Hi @lambertus, the original datetime is formatted with ISO standard. I would try using the DateTimeConverter in this case, as in:

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.

13 replies

takashi
Celebrity
  • 7843 replies
  • Best Answer
  • January 11, 2020

Hi @lambertus, the original datetime is formatted with ISO standard. I would try using the DateTimeConverter in this case, as in:


lambertus
Enthusiast
Forum|alt.badge.img+23
  • Author
  • Enthusiast
  • 141 replies
  • January 11, 2020

Hi @lambertus, the original datetime is formatted with ISO standard. I would try using the DateTimeConverter in this case, as in:

Hi @takashi, thanks for your fast reply! It works almost fine! I converted most records to a suitable output, ISO standard. However, I am left with 33 records which did not contain data. I would like to store them also in the attribute 'Ingebruikname'. Finally, I would like to have an attribute with a ISO standard date or empty.


lars_de_vries
Forum|alt.badge.img+10
  • 388 replies
  • January 11, 2020

I agree with @takashi.

However, you mentioned that you wanted to solve this using RegEx. In that case you can use e.g. @ReplaceRegEx(@Value(ingebruikname),^([0-9]{4}-[0-9]{2}-[0-9]{2}).+$,\\1). What you do is replacing the full string by the (first) set between ().

Another solution could be @Left(@Value(ingebruikname),10). This replaces the attribute value by the first 10 characters.

And you might as well use a DateTime function within an AttributeCreator or -Manager.


lambertus
Enthusiast
Forum|alt.badge.img+23
  • Author
  • Enthusiast
  • 141 replies
  • January 11, 2020

I agree with @takashi.

However, you mentioned that you wanted to solve this using RegEx. In that case you can use e.g. @ReplaceRegEx(@Value(ingebruikname),^([0-9]{4}-[0-9]{2}-[0-9]{2}).+$,\\1). What you do is replacing the full string by the (first) set between ().

Another solution could be @Left(@Value(ingebruikname),10). This replaces the attribute value by the first 10 characters.

And you might as well use a DateTime function within an AttributeCreator or -Manager.

Thanks for your reply! I used the AttributeManager in combination with your RegEx which works fine :)

What does the last part of your RegEx mean: +$\\1 ?

 


lars_de_vries
Forum|alt.badge.img+10
  • 388 replies
  • January 11, 2020

Hi @takashi, thanks for your fast reply! It works almost fine! I converted most records to a suitable output, ISO standard. However, I am left with 33 records which did not contain data. I would like to store them also in the attribute 'Ingebruikname'. Finally, I would like to have an attribute with a ISO standard date or empty.

What is it that you would like to store then, if you have no information?

And if you'd like to keep the original value, you could copy it using a AttributeCopier or -Manager before changing the attribute ingebruikname.


lars_de_vries
Forum|alt.badge.img+10
  • 388 replies
  • January 11, 2020

Thanks for your reply! I used the AttributeManager in combination with your RegEx which works fine :)

What does the last part of your RegEx mean: +$\\1 ?

 

I tried to explain that in the post. It takes the first result that is contained between (). If you just wanted the numbers you could use e.g. @ReplaceRegEx(@Value(ingebruikname),^([0-9]{4})-([0-9]{2})-([0-9]{2}).+$,\\1\\2\\3)

In your example \\1 would be 2019, \\2 would be 01 and \\3 would be 11


lambertus
Enthusiast
Forum|alt.badge.img+23
  • Author
  • Enthusiast
  • 141 replies
  • January 11, 2020

What is it that you would like to store then, if you have no information?

And if you'd like to keep the original value, you could copy it using a AttributeCopier or -Manager before changing the attribute ingebruikname.

My data is not that perfect. I want to leave the fields with no date empty or give them a default value like 9999.


takashi
Celebrity
  • 7843 replies
  • January 11, 2020

Hi @lambertus, the original datetime is formatted with ISO standard. I would try using the DateTimeConverter in this case, as in:

In order to keep empty attribute as-is, set "Yes" to the Passthrough nulls, empties, or missing parameter, shown in the screenshot I posted.

If you are using an old version of FME, the transformer may not have that parameter. In that case, you can just connect both the Output port and the <Rejected> port together to the subsequent transformer.


takashi
Celebrity
  • 7843 replies
  • January 11, 2020

Hi @lambertus, the original datetime is formatted with ISO standard. I would try using the DateTimeConverter in this case, as in:

If you need to replace the attribute value with 9999 when it doesn't start with the correct date format "yyyy-mm-dd", this workflow could also be available, for example.


lambertus
Enthusiast
Forum|alt.badge.img+23
  • Author
  • Enthusiast
  • 141 replies
  • January 11, 2020

If you need to replace the attribute value with 9999 when it doesn't start with the correct date format "yyyy-mm-dd", this workflow could also be available, for example.

Thanks @takashi! I am going to try how I far get.


lambertus
Enthusiast
Forum|alt.badge.img+23
  • Author
  • Enthusiast
  • 141 replies
  • January 11, 2020

If you need to replace the attribute value with 9999 when it doesn't start with the correct date format "yyyy-mm-dd", this workflow could also be available, for example.

Which transformer do you use to combine both these outputs?


arnold_bijlsma
Enthusiast
Forum|alt.badge.img+15
  • Enthusiast
  • 126 replies
  • January 11, 2020

I agree with @takashi: when working with dates and times, really try using the DateTime functions and transformers, rather than RegEx. RegEx is very powerful, but is also known to be a memory hogger when used a bit too leniently; in particular when using 'match-anythings' like .+ and .*


takashi
Celebrity
  • 7843 replies
  • January 11, 2020

Which transformer do you use to combine both these outputs?

It depends on what you need to perform next. If you just want to check the result, for example, connect an Inspector and run.