1
$\begingroup$

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?

$\endgroup$

1 Answer 1

1
$\begingroup$
Clear["Global`*"];
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"

keys = {"Occurrence ID", "Catalog number", 
   "Basis of record Preserved specimen"};

fstr = First@
    StringCases[
     arc110250, # ~~ Repeated[WhitespaceCharacter, {1, ∞}] ~~
       Shortest[___] ~~ EndOfLine | EndOfString] &;

If you just want to isolate the substring only, use hstr instead:

hstr = First@
    StringCases[
     arc110250, # ~~ Repeated[WhitespaceCharacter, {1, ∞}] ~~
        h : Shortest[___] ~~ EndOfLine | EndOfString :> h] &;

Usage:

fstr@"Catalog number"

"Catalog number 110250"

fstr /@ keys

{"Occurrence ID ARC_110250", "Catalog number 110250", "Basis of
record Preserved specimen Life stage juvenile"}

$\endgroup$
2
  • 1
    $\begingroup$ You make it look easy. Now I better understand how to make use of Shortest and Repeated. $\endgroup$ Commented Dec 28, 2023 at 20:52
  • $\begingroup$ @StuartPoss I am glad I was able to help. Thanks for the accept. $\endgroup$
    – Syed
    Commented Dec 29, 2023 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.