Skip to main content
Commonmark migration
Source Link

###Corrected answer

Corrected answer

My original answer (see edit history) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

Or:

"" <> StringSplit[str, Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"]]
"Data 1\n Data 2\nData 3\nData 4"

And the RE equivalent:

First @ StringPattern`PatternConvert[Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"]]
"(?ms)^[@#].*?\n"
"" <> StringSplit[str, RegularExpression["(?ms)^[@#].*?\n"]]
"Data 1\n Data 2\nData 3\nData 4"

###Corrected answer

My original answer (see edit history) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

Or:

"" <> StringSplit[str, Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"]]
"Data 1\n Data 2\nData 3\nData 4"

And the RE equivalent:

First @ StringPattern`PatternConvert[Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"]]
"(?ms)^[@#].*?\n"
"" <> StringSplit[str, RegularExpression["(?ms)^[@#].*?\n"]]
"Data 1\n Data 2\nData 3\nData 4"

Corrected answer

My original answer (see edit history) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

Or:

"" <> StringSplit[str, Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"]]
"Data 1\n Data 2\nData 3\nData 4"

And the RE equivalent:

First @ StringPattern`PatternConvert[Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"]]
"(?ms)^[@#].*?\n"
"" <> StringSplit[str, RegularExpression["(?ms)^[@#].*?\n"]]
"Data 1\n Data 2\nData 3\nData 4"
added 3 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

###Corrected answer

My original answer (see edit history) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ _____ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

Or:

"" <> StringSplit[str, Shortest[StartOfLine ~~ "@" | "#" ~~ _____ ~~ "\n"]]
"Data 1\n Data 2\nData 3\nData 4"

And the RE equivalent:

First @ StringPattern`PatternConvert[Shortest[StartOfLine ~~ "@" | "#" ~~ _____ ~~ "\n"]]
"(?ms)^[@#].+*?\n"
"" <> StringSplit[str, RegularExpression["(?ms)^[@#].+*?\n"]]
"Data 1\n Data 2\nData 3\nData 4"

###Corrected answer

My original answer (see edit history) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ __ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

Or:

"" <> StringSplit[str, Shortest[StartOfLine ~~ "@" | "#" ~~ __ ~~ "\n"]]
"Data 1\n Data 2\nData 3\nData 4"

And the RE equivalent:

First @ StringPattern`PatternConvert[Shortest[StartOfLine ~~ "@" | "#" ~~ __ ~~ "\n"]]
"(?ms)^[@#].+?\n"
"" <> StringSplit[str, RegularExpression["(?ms)^[@#].+?\n"]]
"Data 1\n Data 2\nData 3\nData 4"

###Corrected answer

My original answer (see edit history) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

Or:

"" <> StringSplit[str, Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"]]
"Data 1\n Data 2\nData 3\nData 4"

And the RE equivalent:

First @ StringPattern`PatternConvert[Shortest[StartOfLine ~~ "@" | "#" ~~ ___ ~~ "\n"]]
"(?ms)^[@#].*?\n"
"" <> StringSplit[str, RegularExpression["(?ms)^[@#].*?\n"]]
"Data 1\n Data 2\nData 3\nData 4"
added 126 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

###Corrected answer

My original answer (preserved belowsee edit history) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ __ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

 

Old answer

Please tell me if this works as desiredOr:

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data"" 2\nData<> 3";

StringCases[strStringSplit[str, StartOfLineShortest[StartOfLine ~~ Except["@""@" | "#"]"#" ~~ __ ~~ EndOfLine]"\n"]]
{"Data 1\n Data 2\nData 3"}3\nData 4"

The regular expression for this pattern is more compact but IMO harder to readAnd the RE equivalent:

First @ StringPattern`PatternConvert[StartOfLineStringPattern`PatternConvert[Shortest[StartOfLine ~~ Except["@""@" | "#"]"#" ~~ __ ~~ EndOfLine]"\n"]]
"(?ms)^[^@#]^[@#].+$"+?\n"
StringCases[str"" <> StringSplit[str, RegularExpression["(?ms)^[^@#]^[@#].+$"]]+?\n"]]
{"Data 1\n Data 2\nData 3"}3\nData 4"

###Corrected answer

My original answer (preserved below) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ __ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

 

Old answer

Please tell me if this works as desired:

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3";

StringCases[str, StartOfLine ~~ Except["@" | "#"] ~~ __ ~~ EndOfLine]
{"Data 1\n Data 2\nData 3"}

The regular expression for this pattern is more compact but IMO harder to read:

First @ StringPattern`PatternConvert[StartOfLine ~~ Except["@" | "#"] ~~ __ ~~ EndOfLine]
"(?ms)^[^@#].+$"
StringCases[str, RegularExpression["(?ms)^[^@#].+$"]]
{"Data 1\n Data 2\nData 3"}

###Corrected answer

My original answer (see edit history) was not correct, unless all of your data lines are contiguous.

str = "# Heading 1\n# Heading 2\n@ Heading 3\n@ Heading 4\nData 1\n Data 2\nData 3\n@ \
Heading 5\nData 4";

StringReplace[str, Shortest[StartOfLine ~~ "@" | "#" ~~ __ ~~ "\n"] :> ""]
"Data 1\n Data 2\nData 3\nData 4"

Or:

"" <> StringSplit[str, Shortest[StartOfLine ~~ "@" | "#" ~~ __ ~~ "\n"]]
"Data 1\n Data 2\nData 3\nData 4"

And the RE equivalent:

First @ StringPattern`PatternConvert[Shortest[StartOfLine ~~ "@" | "#" ~~ __ ~~ "\n"]]
"(?ms)^[@#].+?\n"
"" <> StringSplit[str, RegularExpression["(?ms)^[@#].+?\n"]]
"Data 1\n Data 2\nData 3\nData 4"
deleted 416 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
added 442 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
added 281 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading