last day (24 days later) » 

10:31 PM
1
A: How do I use the new line character in R?

r2evansRegex solutions are incredibly sensitive to the formation of the sentences, and since they have irregular spacing I'm inferring that they are either human-generated or generated with an irregular/inconsistent process. Deviations from this pattern will certainly cause portions to break. As such, ...

 
Regular expressions are both powerful and, if misunderstood, significant liabilities. Use them carefully and understand what "no match" can mean. Since you're learning about regexes here, I'll say this: my answer specifically avoided gsub because, without great care, it will happily return the entire phrase (when nothing is found) vice just the portion you originally wanted to extract. I'm sure there can be data that will break this combination of regex-recipes.
 
Thanks a lot. The text file is human generated by it has over 100 lines. This is just one example of files and I have also more than 100 files generated by different centers.
 
Does this answer your question?
 
Can I modify your code to: Text1 = readLines("Text1.txt") dat = strsplit(Text1)[[1]] when I run it, it say Error in strsplit(Text1) : argument "split" is missing, with no default
 
That's not my code (you did an incomplete copy, but you don't need it anyway). If you are reading in your data from a file, then use readLines. The snippet of code I added immediately under "so for sample data" is only to work-around not having your text file. Don't use that if you have a text file.
 
10:31 PM
I'm asking that, because I also want to learn ( to the best of mine) and understand it and so be able to modify it for different cases.
 
If you want to test/try my code to learn it, then you need to use the entire line of code. In the case of strsplit, it requires at least two arguments: the string, and the split pattern. In the case of my sample code, you omitted the second argument "[\n\r]". But again, if you first did Text1 <- readLines(...), then you don't need that line.
What's up, Lionette?
 
Thanks for helping (and baring with) me.
I think I understood most part of your code,

Just this part is a little bit confusing for me.
"(?<=data )[0-9]+(?= is taken)",
"(?<=taken on )\\w+(?=, 2)",
"(?<=, )2[0-9]{3}\\b",
"(?<= at )\\w+(?=\n)",
"(?<=and is ).*(?=significant)",
"(?<=significant with).*"


If you do not mind, cna I ask you to expand a little bit more on it.
 
Each of those is an independent regular expression (regex)
the next chunk of code uses lapply, which iterates over each of those patterns and extracts the matching portion (if any) from each of the grouped lines of combined
The fancy stuff like "(?<=data)" is a regex "lookbehind", meaning it matches things that follow that text but does not include that portion ("data") within the matched pattern; similarly, "(?= is taken)" is "lookahead" and needs that after the text to be returned
as you learn more about regex, you might see that the lookbehind and lookahead parts are not "consumed" by the pattern matching; in the first case, the consuming pattern matching is just "[0-9]+"
If you want to know what "[0-9]+" means, then welcome to regex! There are many good tutorials on regex, and me chatting here will not do them justice (because it truly is a large field) ... I suggest you look at regular-expressions.info to learn what each of the "patterns" strings means
 
Great>
thanks I learned a lot
 
glad I could help!
 
11:01 PM
(if you're still here ... I was typing that a little distractedly, my apologies ... lookahead and lookbehind are reversed in the direction, though still correct in their basic meaning
 

last day (24 days later) »