简体   繁体   English

R-等同于pos函数

[英]R - equivalent of pos function

I have a string in R which is something like: 我在R中有一个字符串,类似于:

File_ABC777_PatientId789.DATA File_ABC777_PatientId789.DATA

or can be something like: File_ABC7878787_234_PatientId892.DATA 或可以类似:File_ABC7878787_234_PatientId892.DATA

I want to extract the part of the string that is in between "PatientId" and ".DATA" ...How do I do this in R ? 我想提取介于“ PatientId”和“ .DATA”之间的字符串部分...如何在R中做到这一点? In C# or other languages this is really easy and it's usually done using a String POS Function ... But I can't seem to find a similar function in R ...any thoughts ? 在C#或其他语言中,这真的很容易,通常是使用String POS Function来完成的...但是我似乎无法在R中找到类似的功能...任何想法?

Cheers !!! 干杯! MadSeb MadSeb

You could use ?sub : 您可以使用?sub

x <- "File_ABC777_PatientId789.DATA"
sub(x=x, pattern="^.*PatientId([0-9]+).DATA$", replacement="\\1")

I'm not sure what function in other languages you are specifically referring to. 我不确定您专门指的是其他语言的功能。 As sgibb mentioned, R has plenty of regex type functions. 如sgibb所述,R具有大量的正则表达式类型的函数。

But if your strings are always in the same format or pattern, you can simply use substr , which simply takes a string, and the start and end positions you want to extract (or replace). 但是,如果您的字符串始终采用相同的格式或模式,则可以简单地使用substr ,该字符串只需要一个字符串,以及要提取(或替换)的开始和结束位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM