regexp help

M

mani

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....
 
M

Martien Verbruggen

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
 
M

mani

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

Sam Holden

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
 
T

Tad McClellan

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.
]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

help with regexp 5
While loop unclear, can someone help? 4
Help 1
Q for a source code in an exercise 1
I dont get this. Please help me!! 2
Needing Help! 1
Processing in Python help 0
Hello and Help please :-) 1

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top