Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: refering to a long pattern by a variable

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57733] Re: [mg57713] refering to a long pattern by a variable
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 7 Jun 2005 02:03:34 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

What about...

txt = "abcbacabzzcz";

pat2char[twochar_String] /; StringLength[twochar] == 2 :=
  With[{chars = Characters[twochar]},
    First[chars] ~~ ___ ~~ Last[chars]]

StringCases[txt, ShortestMatch[pat2char["bc"]], Overlaps -> False]
{bc, bac, bzzc}

Or simplify things even more...

stringmatches[twochar_String /; StringLength[twochar] == 2][txt_] :=
  With[{chars = Characters[twochar]},
    StringCases[txt, ShortestMatch[First[chars] ~~ ___ ~~ Last[chars]],
      Overlaps -> False]]

stringmatches["bc"][txt]
{bc, bac, bzzc}

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/




From: marloo3 at mail15.com [mailto:marloo3 at mail15.com]
To: mathgroup at smc.vnet.net


hello
i wish if i could refer to a long pattren by just a variable,
suppose the:
txt="abcbacabzzcz";
and the pattern to search is "b"~~___~~"c"
StringCases[txt, ShortestMatch["b" ~~ ___ ~~ "c"], Overlaps -> False]
Out[]={bc, bac, bzzc}
ok, i hope there is a way to supply just the :
pat=Characters["bc"];
and some procedure will make the "b" ~~ ___ ~~ "c" from the above pat
variable
and assign this pattern to pat2 variable so we just write:
StringCases[txt,
ShortestMatch[pat2], Overlaps -> False]
to give the output
Out[]={bc, bac, bzzc}
i have tried it by StringJoin but it does not work.
thanks






  • Prev by Date: Re: Finding Position in an ordered list
  • Next by Date: Re: Re: simple set operations
  • Previous by thread: Re: refering to a long pattern by a variable
  • Next by thread: Re: refering to a long pattern by a variable