-1

I have these 5 problems with strings in R:

1) Numbers between the third and fourth dot. Disregard the “0” to the left if any.

ex: I have: 01.030.131.012962.000000 -1/1 - I need: 12962

2) Numbers between the last and penultimate point.

ex: I have: 33.31.17811849.0 I need: 17811849

3) Number between the second and third "/". Disregard the “0” to the left if any.

ex: I have: 2001/96/00000036/0 I need: 36

4) Number after the second dot. Disregard “0”,"." or "-" to the left if any.

ex: I have: 0118.11.54.779-9 I need: 547799

5) 7 numbers after numbers 775, 776 or 746.

ex: I have: 0306920189907750240991000 I need: 0240991

  • 2
    Most regex questions are resolved by reading through https://stackoverflow.com/a/22944075/3358272 and working through each problem. Other good references include https://regexr.com/ and https://regex101.com/. – r2evans Oct 20 '21 at 18:32
  • First one: \d+\.\d+\.\d+\.[0]([1-9]?\d*)\.\d+ – Mossa Oct 20 '21 at 18:42
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 20 '21 at 21:16

1 Answers1

1
gsub("^0", "", strsplit("01.030.131.012962.000000", "[.]")[[1]][4])
# [1] "12962"

gsub("^0", "", tail(strsplit("33.31.17811849.0", "[.]")[[1]], 2)[1])
# [1] "17811849"

gsub("^0+", "", strsplit("2001/96/00000036/0", "/")[[1]][3])
# [1] "36"

strsplit("0118.11.54.779-9", "[-.]")[[1]][3] ## your question is inconsistent here ...
# [1] "54"
strsplit("0118.11.54.779-9", "[-.]")[[1]][-(1:2)]
# [1] "54"  "779" "9"  
strsplit("0118.11.54.779-9", "[-.]")[[1]][3:4]
# [1] "54"  "779"
paste(strsplit("0118.11.54.779-9", "[-.]")[[1]][3:4], collapse = "")
# [1] "54779"

regmatches("0306920189907750240991000", gregexpr("(?<=(775|776|746)).*", "0306920189907750240991000", perl = TRUE))
# [[1]]
# [1] "0240991000"
r2evans
  • 141,215
  • 6
  • 77
  • 149