Re: new for regular expression in Perl




----- Original Message ----- From: "chen li" <chen_li3@xxxxxxxxx>



Hi all,

Here is my problem:

my $string="chen schen";

I want to use regular expression to find the exact
match in the string. So when I want to match "chen" I
expect "chen" only.
But  use the following line I get both "chen" and
"schen" at the same time.
$string=~/chen/g;

How do I get what I expect?

Hi Chen

You can get the results by adding a \b before and after your reg expression. \b is a boundary between a word and a non-word character. (A word character is a-z, A-Z, 0-9, or underscore, _).So, for your example, schen wouldn't match then because the 's' preceding 'c' is a word character and so the \b wouldn't be true. But, it would match chen because the (non) character (beginning of the string) preceding the 'c' would make \b true.

$string=~/\bchen\b/g;

MATCH
"chen "
"#chen"
"here is a chen and another chen"
"chen's"    (the apostrophy is a non-word char)

NO MATCH
"schen"
"chens"

Chris


.



Relevant Pages

  • Re: regular expression question
    ... "Syntax" means nothing to Regular ... public string Opened() ... and be able to express them in Regular Expression syntax. ... of word characters, and as I said before, '<' is not a word character. ...
    (microsoft.public.dotnet.general)
  • 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)
  • Re: How do I evaluate a JSON response?
    ... There cannot be such a regular expression in ECMAScript as it does ... "7.8.4 String Literals". ... had two different kinds of character escape sequences in one character ... Prototype.js was written by people who don't know javascript for people ...
    (comp.lang.javascript)
  • Re: Regular expression optimization
    ... position in the replacement array of strings, ... > input string and a MatchEvaluator delegate. ... > The first part required combining the separate Regular Expression strings ...
    (microsoft.public.dotnet.general)