Solved

Anyone know what the RegEx expression would be ...?


I am trying to get all of the Characters in a string that occur before the following " /" (thats a space followed by a /). Here is the catch there could be spaces and / before the occurance of the " /" and I want to keep those in the found string.

I was trying to use the stringSearch and do this in one step (I was able to do this in 2 steps by first stringReplace the " /" with a character I knew would not appear and then doing a stringsearch but this should be doable in one step with the correct regEx expression)

Here are examples

123ABD / 99 - "123ABD" would be found

123/ABD / 99 - "123/ABD" would be found

123% ABD/99 / 789 - "123% ABD/99" would be found

123 / 456 / 789 - "123" would be found

hopefully this makes it clear

Thx

in advance to you RegEx wizards out there...

icon

Best answer by gio 8 April 2020, 18:17

View original

20 replies

Badge +2

1586283947463 

where the regex is: ^(.+)\\s/\\s

and the result is returned as the list:

_substring{0}.part 123 / 456

FME 2020: regexExample.fmw

Userlevel 1
Badge +21

You  could do this in an attributecreator

 

@Left(@Value(string),@FindRegEx(@Value(string),\s/))

This finds the position of the first " /", then uses that position to get the left characters up until that point

1586283947463 

where the regex is: ^(.+)\\s/\\s

and the result is returned as the list:

_substring{0}.part 123 / 456

FME 2020: regexExample.fmw

this fails on the example "123 / 456 / 789" it returned "123 / 456" it should only return "123"

1586283947463 

where the regex is: ^(.+)\\s/\\s

and the result is returned as the list:

_substring{0}.part 123 / 456

FME 2020: regexExample.fmw

but still way closer than I ever got :)

 

You  could do this in an attributecreator

 

@Left(@Value(string),@FindRegEx(@Value(string),\s/))

This finds the position of the first " /", then uses that position to get the left characters up until that point

thanks not the way I was thinking to do it but it does work

Userlevel 2
Badge +17

Hi @chris.taylor, there should be several solutions. This regex is one of them, which matches one or more consecutive characters before the first "<space>/".

.+?(?= /)
Badge +3

but still way closer than I ever got :)

 

RegEx is by default 'greedy' (finding the longest match possible), and you need to make it 'lazy' (finding the shortest match possible) by adding a question mark after the plus (also shown in @takashi's answer):

^(.+?)\s/\s 
Userlevel 1
Badge +21

thanks not the way I was thinking to do it but it does work

In an AttributeCreator/Manager you could also replace the " /" and everything to the end of the string with nothing to get the same result

@ReplaceRegEx(@Value(input),\s\/.*$,"")

 

Hi @chris.taylor, there should be several solutions. This regex is one of them, which matches one or more consecutive characters before the first "<space>/".

.+?(?= /)

thanks I figured there were multiple ways of doing this but I stink at Regex expressions so I was interested in learning how it would be done that way...   I have several ways now thanks

1586283947463 

where the regex is: ^(.+)\\s/\\s

and the result is returned as the list:

_substring{0}.part 123 / 456

FME 2020: regexExample.fmw

thanks Ill check that out today

Badge +3

@chris.taylor

 

Hi. Here is one that actually does the job. That is, it also gets the last one right.

 

 

^(.*?)(?=(?: \\/))

 

Badge +3

@chris.taylor

 

Hi. Here is one that actually does the job. That is, it also gets the last one right.

 

 

^(.*?)(?=(?: \\/))

 

Have you actually tried it in a StringSearcher, because it fails on all 4 examples for me.

stringsearcherregex.fmw

Userlevel 1
Badge +21

Have you actually tried it in a StringSearcher, because it fails on all 4 examples for me.

stringsearcherregex.fmw

You have extra spaces on the end of your regular expression.

^(.+?)\s/\s   so this is including the " / " is there a way to return only the characters before the " / " ?
 
Badge +3

You have extra spaces on the end of your regular expression.

You're right! Doh!!

@chris.taylor

 

Hi. Here is one  that actually does the job. That is, it also gets the last one right.

 

 

^(.*?)(?=(?: \/))

 

BINGO this does exactly what I was after but I havr to admit I have no clue as to why...   Ill do some research on this expression ... thanks
Badge +3
^(.+?)\s/\s   so this is including the " / " is there a way to return only the characters before the " / " ?
 

You need to use the _substring list that @markatsafe is showing and then take your value as the first element from that list, i.e. _substring{0}.part

BINGO this does exactly what I was after but I havr to admit I have no clue as to why...   Ill do some research on this expression ... thanks

wait no it doesn't get it right   it still shows "123 / 456"  this one should return 123

 

123 / 456 / 789    --- 123 / 456

 

123% ABD/99 / 789   ----    123% ABD/99

 

123/ABD / 99    ---    123/ABD

 

123ABD / 99    ---  123ABD

 

but this worked (just added the lazy ? in the first part as was suggested in one of the other comments):

^(.*?)(?=(?:\/))

 

 

string    _creation_instance    _first_match

 

123 / 456 / 789    0    123

 

123% ABD/99 / 789    0    123% ABD

 

123/ABD / 99    0    123

 

123ABD / 99    0    123ABD

 

 

 

wait no it doesn't get it right it still shows "123 / 456" this one should return 123

 

123 / 456 / 789 --- 123 / 456

 

123% ABD/99 / 789 ---- 123% ABD/99

 

123/ABD / 99 --- 123/ABD

 

123ABD / 99 --- 123ABD

 

but this worked (just added the lazy ? in the first part as was suggested in one of the other comments):

^(.*?)(?=(?:\\/))

 

 

string _creation_instance _first_match

 

123 / 456 / 789 0 123

 

123% ABD/99 / 789 0 123% ABD

 

123/ABD / 99 0 123

 

123ABD / 99 0 123ABD

 

 

 

wait you diid have that ? in there I must have missed it when I typed it in the first time..... YOU got it problem solved thanks

Badge +2

@chris.taylor this is a great regex site f understanding what each part of a regex expression is doing.: https://regexr.com/

Reply