CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Regex help: match word just to the right

 



perlFun
Novice

Jul 17, 2013, 4:28 PM

Post #1 of 2 (451 views)
Regex help: match word just to the right Can't Post

Say I have the following:

my $line = 'Module A Serial Number: 8002276940\n'

I have a regex as follows: my $reg1= '^Module'; with the goal of bringing back 'A' (because 'A' is directly to the right of this). I am just curious what's the best way to do this. I know I need to use the search 's/' operator, but I'm curious how to get what lies to the right. If there's any page you can refer me to about this, that would just as helpful. The regex stuff I seem to find seems to just show me how to do pattern matching itself. In this, I can match the pattern but I'm more interested in what directly follows the pattern (as in the next word, meaning there should be whitespace separating them)

Thanks so much!


Laurent_R
Veteran / Moderator

Jul 17, 2013, 11:28 PM

Post #2 of 2 (445 views)
Re: [perlFun] Regex help: match word just to the right [In reply to] Can't Post

The s/// operator is not a search operator, but a substitution operator. It should not be used in the context you discribed.

A regex is just a textual description of what you are looking for, with a few symbols to represent multiplie chartacters, alternation, etc.

But you need to described a bit more what you want to match. Suppose you just want to match a single letter on the right of the word module (separated by a space), you can do this:


Code
my $line = 'Module A Serial Number: 8002276940\n'; 
my $matched_char = $1 if $line =~ /Module (\w)/;


"\w" represents any alphanumerix character, the parens () specify that you want to capture the content, and the capture is made into the $1 special variable (if there was a second set of parens, the capture would be into $2, etc.).

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives