I have extracted and stored some plaintext strings (derived from a pdfs) from the web that I wish to search for various values following certain text substrings that are different for different records.
An example of a partial record is as follows:
arc110250 = "Data resource Atlantic Reference Centre
Institution code Atlantic Reference Centre
Supplied institution code The Atlantic reference Centre
Collection code Atlantic Reference Centre Museum of Canadian Atlantic
Organisms
Catalog number 110250
Occurrence ID ARC_110250
Basis of record Preserved specimen
Life stage juvenile"
In InputForm it looks like this (Taking the first 750 or so characters):
"12/27/23, 1:28 PM Record: ARC:110250 | Occurrence record | Canadensys
explorer\nhttps://data.canadensys.net/explorer/occurrences/a4a842ba-c08c-4560-9301-027e40cf8dcd 1/5\nOccurrence
record:\nARC:110250\nPreservedSpecimen of Sebastes Cuvier,\n1829 | rockcod recorded on 1978-08-22\nCanadensys\nDate loaded: 2019-12-09\nDate
last processed: 2019-12-09\nDataset\nEvent\nTaxonomy\nGeospatial\nAdditional properties\nData quality tests (1 :f057, 2 :f06a, 28 :f058, 11 :f059, 44
:f05e)\nDataset\nData partner Canadian node of the Ocean Biogeographic Information\nSystem\n+\n-\nLeaflet | Map data © OpenStreetMap, imagery ©
CartoDB\n12/27/23, 1:28 PM Record: ARC:110250 | Occurrence record | Canadensys
explorer\nhttps://data.canadensys.net/explorer/occurrences/a4a842ba-c08c-4560-9301-027e40cf8dcd 2/5\nData resource Atlantic Reference
Centre\nInstitution code Atlantic Reference Centre\nSupplied institution code "The Atlantic reference Centre"\nCollection code Atlantic
Reference Centre Museum of Canadian Atlantic\nOrganisms\nCatalog number 110250\nOccurrence ID ARC_110250\nBasis of record Preserved
specimen\nLife stage juvenile\n"
I want to search for the substrings, such as "Catalog number", or "Occurrence ID" and retrieve back a substrings containing "Catalog number 110250" and "Occurrence ID ARC_110250" respectively. That is the strings or digits on the same line immediately following the requested "key words".
When I try for example:"
occurrenceID = StringCases[arc110250, StartOfLine ~~ "Occurrence
ID" ~~ __ ~~ Except[EndOfLine]]
I get back the substring I want as the first entry in output, but then I also get all the text that follows on subsequent lines of the text string:
{"Occurrence ID ARC_110250
Basis of record Preserved specimen
Life stage juvenil"}
Same is true if I try to get the strings following "Catalog number":
catalognumber = StringCases[arc110250, StartOfLine ~~ "Catalog
number" ~~ __ ~~ Except[EndOfLine]]
This returns:
{"Catalog number 110250
Occurrence ID ARC_110250
Basis of record Preserved specimen
Life stage juvenil"}
Using Except[] or not using Except[] seems to make no difference. I've tried to use RegularExpression[], but my efforts with weak skills with RegEx returns no substrings at all. For example:
icoderegex = StringCases[arc110250,
RegularExpression["^Institution" ~~ "\\s+" ~~ "code" ~~ ".*"]]
How can I get StringCases to return just the substring that includes only the first line of the output and not those of the other lines that follow? In other words, how can I get the pattern to terminate in the appropriate place?