Skip to main content
Solved

Keep only part of string matching Regex

  • February 17, 2021
  • 4 replies
  • 1854 views

lambertus
Enthusiast
Forum|alt.badge.img+12

A dataset of 900 records contains a string attribute. This string varies in length and contains always a X- and Y-coordinate.

 

Example:

":5.781699137962022,"rijksdriehoekX":181934.095,"rijksdriehoekY":474580.092,"rijksdriehoekZ":0

 

I just want to get the part in bold and created a Regex to select this part. My idea is to replace the complete string and just keep the string part with the coordinates (bold part).

 

My Regex: rijksdriehoekX":[0-9]{6}.[0-9]{3},"rijksdriehoekY":[0-9]{6}.[0-9]{3}

Any ideas how to solve this issue?

 

Best answer by hkingsbury

lambertus wrote:

Thanks @hkingsbury​ ! The regex works perfect. I have filtered now all records which contain coordinate values. 

 

However, I still need to subtract these coordinates from the string per record and turn them into attribute values in such a way they can be used in a VertexCreator. Do you have any suggestions? 😃

 

My goal, using the example above, get finally 2 columns with x and y coordinates.

 

2021-02-17 21_57_45-Clipboard

A small change to the regex can achieve that, adding two extra capture groups around the coords

(rijksdriehoekX":(\d{6}\.\d{3}),"rijksdriehoekY":(\d{6}\.\d{3}))

make sure you set the subexpression list name under advanced in the stringsearcher

 

Screenshot 2021-02-18 100358 

View original
Did this help you find an answer to your question?

4 replies

hkingsbury
Celebrity
Forum|alt.badge.img+53
  • Celebrity
  • February 17, 2021

You're really close. I've made a couple of minor changes to your regex

(rijksdriehoekX":\d{6}\.\d{3},"rijksdriehoekY":\d{6}\.\d{3})
  1. adding a capture group around the whole statement "(...)"
  2. switch [0-9] to \d
  3. escaped '.'. In regex a '.' means any character. by escaping it with a '\' you will only match a '.'

to extract that match, use a StringSearcher. It there is a match it will come out on the '_first_match' attribute

 

One thing I did note, are your coordinates always going to be 6 long followed by 3dp? you may want to look at adding a range, especaily for the decimal places. {1,3} will match 1 to 3 numbers long


lambertus
Enthusiast
Forum|alt.badge.img+12
  • Author
  • Enthusiast
  • February 17, 2021
hkingsbury wrote:

You're really close. I've made a couple of minor changes to your regex

(rijksdriehoekX":\d{6}\.\d{3},"rijksdriehoekY":\d{6}\.\d{3})
  1. adding a capture group around the whole statement "(...)"
  2. switch [0-9] to \d
  3. escaped '.'. In regex a '.' means any character. by escaping it with a '\' you will only match a '.'

to extract that match, use a StringSearcher. It there is a match it will come out on the '_first_match' attribute

 

One thing I did note, are your coordinates always going to be 6 long followed by 3dp? you may want to look at adding a range, especaily for the decimal places. {1,3} will match 1 to 3 numbers long

Thanks @hkingsbury​ ! The regex works perfect. I have filtered now all records which contain coordinate values. 

 

However, I still need to subtract these coordinates from the string per record and turn them into attribute values in such a way they can be used in a VertexCreator. Do you have any suggestions? 😃

 

My goal, using the example above, get finally 2 columns with x and y coordinates.

 

2021-02-17 21_57_45-Clipboard


hkingsbury
Celebrity
Forum|alt.badge.img+53
  • Celebrity
  • Best Answer
  • February 17, 2021
lambertus wrote:

Thanks @hkingsbury​ ! The regex works perfect. I have filtered now all records which contain coordinate values. 

 

However, I still need to subtract these coordinates from the string per record and turn them into attribute values in such a way they can be used in a VertexCreator. Do you have any suggestions? 😃

 

My goal, using the example above, get finally 2 columns with x and y coordinates.

 

2021-02-17 21_57_45-Clipboard

A small change to the regex can achieve that, adding two extra capture groups around the coords

(rijksdriehoekX":(\d{6}\.\d{3}),"rijksdriehoekY":(\d{6}\.\d{3}))

make sure you set the subexpression list name under advanced in the stringsearcher

 

Screenshot 2021-02-18 100358 


lambertus
Enthusiast
Forum|alt.badge.img+12
  • Author
  • Enthusiast
  • February 18, 2021

Thanks @hkingsbury​ ! Works perfect 😃


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings