Solved

Keep only part of string matching Regex

  • 17 February 2021
  • 4 replies
  • 289 views

Badge +4

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?

 

icon

Best answer by hkingsbury 17 February 2021, 22:05

View original

4 replies

Userlevel 5
Badge +29

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

Badge +4

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

Userlevel 5
Badge +29

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 

Badge +4

Thanks @hkingsbury​ ! Works perfect 😃

Reply