log in | register | forums
Show:
Go:
Forums
Username:

Password:

User accounts
Register new account
Forgot password
Forum stats
List of members
Search the forums

Advanced search
Recent discussions
- Elsear brings super-fast Networking to Risc PC/A7000/A7000+ (News:)
- Latest hardware upgrade from RISCOSbits (News:)
- RISCOSbits releases a new laptop solution (News:4)
- Announcing the TIB 2024 Advent Calendar (News:2)
- RISC OS London Show Report 2024 (News:1)
- Code GCC produces that makes you cry #12684 (Prog:39)
- Rougol November 2024 meeting on monday (News:)
- Drag'n'Drop 14i1 edition reviewed (News:)
- WROCC November 2024 talk o...ay - Andrew Rawnsley (ROD) (News:2)
- October 2024 News Summary (News:3)
Latest postings RSS Feeds
RSS 2.0 | 1.0 | 0.9
Atom 0.3
Misc RDF | CDF
 
View on Mastodon
@www.iconbar.com@rss-parrot.net
Site Search
 
Article archives
Acorn Arcade forums: Programming: Perl pattern matching question
 
  Perl pattern matching question
  ksattic (22:35 29/7/2003)
  Matthew (23:27 29/7/2003)
    ksattic (15:15 30/7/2003)
 
Simon Wilson Message #44965, posted by ksattic at 22:35, 29/7/2003
ksattic
Finally, an avatar!

Posts: 1291
Been mucking around with Perl today and I came across a pattern that I think should match a string, but doesn't.

Try the following:

#!/usr/bin/perl

$match = " D";

if($match =~ /^[\t| {8}]([A-Z|a-z|0-9]+).*/) {
print "Matched\n";
}

With Perl 5.005 (on RISC OS) and 5.6.1 (on a Sun box), the line does not match. Funnily enough, the following does match:

#!/usr/bin/perl

$match = " D";

if($match =~ /^[\t| {8}]/) {
print "Matched\n";
}

Can anyone please explain why the first does not match? Why does changing the eight spaces in the $match string to a tab cause it to match fine?
  ^[ Log in to reply ]
 
Matthew Somerville Message #44967, posted by Matthew at 23:27, 29/7/2003, in reply to message #44965
Matthew

Posts: 520
Can anyone please explain why the first does not match?
You're using classes - [ and ] - where you mean to use normal brackets - ( and ). Your first line says "Match *one* of (tab, vertical bar, space, brace, or the digit 8) at the start of a string, followed by one or more of A-Z, a-z, 0-9 or vertical bar." So eight spaces followed by a D will not match, whereas a single tab followed by a D will. Changing the first set of square brackets to normal brackets will mean it does as you want.

Note you do not need to use | inside square brackets - you can simply say [A-Za-z0-9] - and that nearly everything inside [] is taken literally.
  ^[ Log in to reply ]
 
Simon Wilson Message #44980, posted by ksattic at 15:15, 30/7/2003, in reply to message #44967
ksattic
Finally, an avatar!

Posts: 1291
You're using classes - [ and ] - where you mean to use normal brackets - ( and )
Thanks for the help! I'd got confused somewhere along the way and though that [ and ] were the right way to go.
  ^[ Log in to reply ]
 

Acorn Arcade forums: Programming: Perl pattern matching question