4
$\begingroup$

I'm trying to create a string pattern which will match either (with quotes included):

  • "x by y"
  • x by y
  • 'x by y'

I've tried to use the following pattern: "\"" ~~ ___ ~~ "by" ~~ ___ ~~ "\"" which only matches something with " and tried to find an "or" symbol similar to regex | but to no avail.

Question: What symbols / tools can I use to develop a pattern which matches the above cases?

Question: What is the "correct" translation from a regex approach to a Mathematica approach? Are they the same?

$\endgroup$
1
  • $\begingroup$ For string patterns there is also | (Alternatives) as can be seen here where regex - which can also be used directly - are compared with string patterns. $\endgroup$
    – gwr
    Commented Feb 7, 2019 at 7:29

1 Answer 1

3
$\begingroup$

Here is how I would do it:

quotePattern = { "\"" , "'", "" };
stringPattern = quotePattern ~~ __ ~~ "by" ~~ __ ~~ quotePattern;

StringMatchQ[ #, stringPattern ] & /@ { "x by y", "\"x by y\"", "'x by y'" }

{ True, True, True }

Edit

Be careful what you wish for ... as they say: The OP wants to extract "title by author" from a list of strings I assume. For example:

string = "Some surrounding text. \"The Machine That Changed the World\" by Womack, Jones, and Roos. And it fails.";

Now the above pattern will of course not work. We have to be more precise:

quotePatternMod = { "\"", "'" };
StringCases[ string,
    Shortest[a : quotePatternMod ~~ b : __ ~~ c : "by" ~~ d : ___ ~~ "."] :> a ~~ b ~~ c ~~ d
] 

{"\"The Machine That Changed the World\" by Womack, Jones, and Roos"}

$\endgroup$
6
  • $\begingroup$ I have a follow up question, if you don't mind. I'm trying to use StringCases using the pattern you provided to this string "Some surrounding text. \"The Machine That Changed the World\" by \ Womack, Jones, and Roos. And it fails." And it fails to extract the matching string. Am I using something wrong? $\endgroup$
    – Jeel Shah
    Commented Feb 7, 2019 at 7:50
  • 1
    $\begingroup$ @JeelShah You will have to add Shortest to the pattern otherwise the whole string will match. $\endgroup$
    – gwr
    Commented Feb 7, 2019 at 7:54
  • $\begingroup$ @Jeel What should be the string that is extracted in the case you gave? If it were "booktitle by authors" then you might want to go for a period at the end. $\endgroup$
    – gwr
    Commented Feb 7, 2019 at 8:02
  • $\begingroup$ It should be "The Machine That Changed the World" by Womack, Jones, and Roos. and only that. I changed the pattern to: Shortest[quotePattern ~~ __ ~~ quotePattern ~~ "by" ~~ ___ ~~ "."] which removes the "And it fails." however, it keeps the preceding string. Is there a way to force that only something which fits that pattern completely is selected? I tried putting "Some surrounding text." in quotes and that was also extracted. StringCases["\"Some surrounding text.\" \"The Machine That Changed \ the World\" by Womack, Jones, and Roos [0]. And it fails.", \ stringPattern] $\endgroup$
    – Jeel Shah
    Commented Feb 7, 2019 at 8:06
  • 1
    $\begingroup$ @Jeel name : pattern simply names a pattern you can find it under Pattern in the documentation. $\endgroup$
    – gwr
    Commented Feb 7, 2019 at 8:30

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.