Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regexp help

Reply
Thread Tools

regexp help

 
 
mani
Guest
Posts: n/a
 
      09-30-2003
Hi,

I have a problem with finding the regexp for this

Let us say

$x="# I have been";

if($x!~/^#/)
{
#stuff here
}

will make the code not get into the control loop for the current value
of x because $x starts with #. This is fine but if i give

$x=" #I have been\n";

then the if condition would be true. I want to avoid this I want to
ignore anything that starts with a <space># or # at the start. How do
i do it

I tried /s*^#/ which does not give what i want....

please help me....
 
Reply With Quote
 
 
 
 
Martien Verbruggen
Guest
Posts: n/a
 
      09-30-2003
On 30 Sep 2003 05:29:08 -0700,
mani <(E-Mail Removed)> wrote:

> if($x!~/^#/)


> I want to
> ignore anything that starts with a <space># or # at the start.


> I tried /s*^#/ which does not give what i want....


/^ ?#/

Or, if you meant "any whitespace" instead of "a <space>"

/^\s?#/

Or, if you meant any number of whitespace

/^\s*#/

And, please, don't just copy this, but read the perlre documentation,
and read about what I just told you. It'll help you solve these sorts of
problems yourself in the future.

Martien
--
|
Martien Verbruggen | Blessed are the Fundamentalists, for they
| shall inhibit the earth.
|
 
Reply With Quote
 
 
 
 
Martien Verbruggen
Guest
Posts: n/a
 
      09-30-2003
On Tue, 30 Sep 2003 18:11:18 +0530,
Kasp <(E-Mail Removed)> wrote:
>> then the if condition would be true. I want to avoid this I want to
>> ignore anything that starts with a <space># or # at the start. How do
>> i do it
>>
>> I tried /s*^#/ which does not give what i want....

> -----------^...this should be \s.
> Try this regular expression:
> /^(\s)*#/


Why the capturing parentheses?


Martien
--
|
Martien Verbruggen | Make it idiot proof and someone will make a
| better idiot.
|
 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      09-30-2003
In article <blbtli$4b7$(E-Mail Removed)>, Kasp wrote:
[cut]
>> I tried /s*^#/ which does not give what i want....

> -----------^...this should be \s.
> Try this regular expression:
> /^(\s)*#/


No need to parenthasize the \s.

--
Andreas Kähäri
 
Reply With Quote
 
mani
Guest
Posts: n/a
 
      10-01-2003
Thanks a lot for helping me. As i see the solution is /^\s*#/

I have questions. Martien, I am ok at reading documents but sometimes
i find it better discussing with some good people like you guys .
How does that \s help?. I mean what does it mean?..... Can u tell me?.
I thought it is

/^s*#/.. My interpretation is anything that starts with a space or any
number of space followed by a hash and if it is the beginning and so
this regexp suffices but you guys have said /^\s*#/ why this \?????


Martien Verbruggen <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)> ...
> On 30 Sep 2003 05:29:08 -0700,
> mani <(E-Mail Removed)> wrote:
>
> > if($x!~/^#/)

>
> > I want to
> > ignore anything that starts with a <space># or # at the start.

>
> > I tried /s*^#/ which does not give what i want....

>
> /^ ?#/
>
> Or, if you meant "any whitespace" instead of "a <space>"
>
> /^\s?#/
>
> Or, if you meant any number of whitespace
>
> /^\s*#/
>
> And, please, don't just copy this, but read the perlre documentation,
> and read about what I just told you. It'll help you solve these sorts of
> problems yourself in the future.
>
> Martien

 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      10-01-2003
On 30 Sep 2003 22:04:53 -0700, mani <(E-Mail Removed)> wrote:
> Thanks a lot for helping me. As i see the solution is /^\s*#/
>
> I have questions. Martien, I am ok at reading documents but sometimes
> i find it better discussing with some good people like you guys .
> How does that \s help?. I mean what does it mean?..... Can u tell me?.
> I thought it is
>
> /^s*#/.. My interpretation is anything that starts with a space or any
> number of space followed by a hash and if it is the beginning and so
> this regexp suffices but you guys have said /^\s*#/ why this \?????


s matches 's'.
\s matches whitespace.

perldoc perlre

--
Sam Holden

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-01-2003
mani <(E-Mail Removed)> wrote:


> Martien, I am ok at reading documents but sometimes
> i find it better discussing with some good people like you guys .



Reading the applicable docs, then posting if you don't understand is OK.

Asking us to read the docs for you is not OK.

(there is one of "you" and thousands of "us". Not an efficient use of time.)


> How does that \s help?. I mean what does it mean?.....



The docs for regular expressions say:

perldoc perlre

\s Match a whitespace character


> Can u tell me?.



Yes. It will match a whitespace character.


> I thought it is



Guessing at a language's features is no way to go about
software development.


> /^s*#/.. My interpretation is anything that starts with a space



No, that is anything that starts with the character "s".



[ snip full-quote.
Please see the Posting Guidelines that are posted here frequently.
]

--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matìj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments