Re: String Parsing



Dan wrote:
I am just starting to learn some parsing techniques in Perl.. Can
anyone give me an example of how I might parse a string, replacing
certain instances of words with other words?  For example, replacing
"Ave." with "Avenue", etc.

It's not "parsing" in the broad sense, but rather "matching a regular expresison" in Perl lingo (though I wouln't be surprised if anyone came up with a regular expression matching a valid sentence in modern English B-{).


$string = 'Madison Ave.';
$string =~ s/Ave\./Avenue/;
print "$string\n";

Note that regular expressions in Perl are extremely powerful (more powerful than a megaton of TNT) and the "s" operator has numerous options to control how matching (e.g. "greedy" vs "non greedy") and replacing (e.g. "once" vs. "always") is done.

You should definitely read
perldoc perlop
and
perldoc perlre

--
Josef Möllers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett

.



Relevant Pages

  • Re: String - Counting occurences of a character in it
    ... for more complex string manipulation: ... > Under the CLR I thought the replace returned a string. ... lost as to what you are saying with the regular expression. ... >> same type thing short of replacing an item with spaces then subtracting ...
    (microsoft.public.dotnet.framework.clr)
  • Re: js Newbie
    ... >>To generate a string S2 which is the result of replacing all the vowels ... > I was thinking of suggesting the use of a regular expression, ...
    (microsoft.public.scripting.jscript)
  • Re: String - Counting occurences of a character in it
    ... you could use a regular expression before the Replacemethod and you could ... same type thing short of replacing an item with spaces then subtracting the ... lengths of the string to get my value. ... > Is there a function/method that will do this for me? ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Get regular expression
    ... own tree structure. ... Expression compares a string character-by character, ... regular expression solution, which was about as close as one could get to ... the structure of the hierarchy can be inferred by using ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Get regular expression
    ... regular expression solution, which was about as close as one could get to ... first string. ... explode "ABLATION" and see subnodes of "ENDOMETRIAL ... "Heart 27.33/2" ...
    (microsoft.public.dotnet.languages.csharp)