-2

I have a text with comma and I need count of number characters until comma, because i want select the interval before comma.

Example:
text: "12345, Supply"
I want select just "12345".
Is it possible?

tks!

tzot
  • 92,761
  • 29
  • 141
  • 204

2 Answers2

1

With sqldf, one possibility is this

df <- data.frame(text = rep("12345, Supply", 2))

           text
1 12345, Supply
2 12345, Supply

sqldf("select substr(text, 1, instr(text, ',') - 1) as text from df")

   text
1 12345
2 12345
erocoar
  • 5,723
  • 3
  • 23
  • 45
  • Sorry, it don't work. Look my base: DocNum Code Date Dep PK Amount Text 57434 5 26/09/2016 1280 D 112084,53 "10-1","Supply","text" I need extract only the number before "," of field Text – Fernando M Mar 09 '18 at 13:14
  • You need to post some sample data then / what doesn't work – erocoar Mar 09 '18 at 13:16
  • It does answer the question asked. Please fix your question if you did not ask what you intended. – G. Grothendieck Mar 09 '18 at 14:21
0

We can use sub to match the , followed by one or more spaces and other characters and replace it with blank ("")

sub(",\\s+.*", "", "12345, Supply")
#[1] "12345"

Or capture as a group and replace with the backreference

sub("^([^,]+),.*", "\\1", "12345, Supply")
#[1] "12345"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Sorry, it don't work. Look my base: DocNum Code Date Dep PK Amount Text 57434 5 26/09/2016 1280 D 112084,53 "10-1","Supply","text" I need extract only the number before "," of field Text – Fernando M Mar 09 '18 at 13:22