Skip to main content
Question

How do I find the first space after the 19th character using a regular expression?

  • August 24, 2017
  • 5 replies
  • 408 views
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.

5 replies

davideagle
Contributor
Forum|alt.badge.img+22
  • Contributor
  • August 24, 2017

Use the StringSearcher Transformer and match the first 19 characters with:

^.{19}

or to match just the first space in the string use:

^[^\\s]*\\K\\s

...though that won't exactly count 19 before it matches the space, it just matches the first space in the string.

Hope that helps


davideagle
Contributor
Forum|alt.badge.img+22
  • Contributor
  • August 24, 2017
Looks like a duplicate entry for anyone else looking at this, see also https://knowledge.safe.com/questions/51514/splitting-attributes-by-number-of-words.html which I discovered after answering.

 

 


danilo_fme
Celebrity
Forum|alt.badge.img+52
  • Celebrity
  • August 24, 2017
Looks like a duplicate entry for anyone else looking at this, see also https://knowledge.safe.com/questions/51514/splitting-attributes-by-number-of-words.html which I discovered after answering.

 

 

Hi @1spatialdave, yes you are correct. I belive too is a duplicate entry here.

 

So Good job your answer below.

 


erik_jan
Contributor
Forum|alt.badge.img+26
  • Contributor
  • August 24, 2017

I would use two steps:

Use a SubstringExtractor transformer to find the part after character 19 (start 19 end -1).

Then do the StringSearcher (looking for the first space in the substring). If you add 19 to the _index you will have the location of the first space after position 19 in the original attribute.


Forum|alt.badge.img
  • August 24, 2017

Hi @gmbutler2

 

one more approach would be to:

  • find all spaces in the source string using StringSearcher (the 'found spaces' together with their positions in the source string will be stored in as a list where each item has two elements: match and startIndex);
  • find list item with startIndex > 19 using List Searcher and save it as an attribute using ListIndexer;
  • create two new attributes - one with the beginning of the string till the space found after 19th position of the string and another with the rest of the string:
    • _firstPart = @Left(@Value(_address),@Value(startIndex))
    • _secondPart = @Substring(@Value(_address),@Value(startIndex)+1,-1)

Please take a look at the attached workspace.

This solution is not as elegant as what was suggested before, but if you are not a regex fan you might like it :)