Regular Expressions language R
2
0
Entering edit mode
@fabienchambodut-8213
Last seen 8.8 years ago
United Kingdom

Hi everybody,

I have some difficulties to use regular expressions in R. 
My problem is that I can't recover in a vector the data that I want to recover. 

My R script is the next:

variable<-file("BestFitnessSBML_L1V2_exported.xml", open="r")
while (length(line <- readLines(variable, n = 1))) {
  results<-grep('.*<parameter name=\"a1\" value=\"0.1\"/>.*', line, perl = TRUE, ignore.case = TRUE) 
  print(results)
}
close(fichier)

When I print results I see that the regular expression has been found in my file.

I want to recover a1 and 0.1 in the regular expressions '.*<parameter name=\"a1\" value=\"0.1\"/>.*'. Maybe someone can explain me how to recover it ?
 

Cordially,

Fabien

 

 

regular expressions R R regular expressions • 1.0k views
ADD COMMENT
1
Entering edit mode
@martin-morgan-1513
Last seen 6 days ago
United States

Diego is right to suggest that using regular expressions is not the right way to go. Instead read the file using the XML package

library(XML)
xml = xmlParse("BestFitnessSBML_L1V2_exported.xml")

 and then query it using an 'xpath', with the Abbreviate Syntax of the specification a surprisingly helpful place to start. You might be able to extract the name / value pairs as

name = sapply(xml["//parameter", xmlAttrs, "name")
value = as.numeric(sapply(xml["//parameter", xmlAttrs, "value"))

There are SBML parsing facilities in Bioconductor (rsbml, SBMLR) but I'm not familiar enough with these to know whether they are helpful in this case.

ADD COMMENT
0
Entering edit mode

Nice example, I always end up doing things more complicated than it should with the XML package- this is simpler. I think you are missing closing square brackets after "//parameter" in both calls?

ADD REPLY
0
Entering edit mode
Diego Diez ▴ 760
@diego-diez-4520
Last seen 3.5 years ago
Japan

You are trying to parse XML with regular expressions but maybe you should use the XML package instead? This reminds me of this (famous) question and answer (the top one) in SO (which you may be interested in checking): http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags

ADD COMMENT

Login before adding your answer.

Traffic: 607 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6