0

i have a script in which i need to skip comments, and i am able to achieve it partially...

IN :

{****************************
{test : test...test }

Script:

if ( $data =~  m/(^{\*+$)/ ){
}

With the above match i am able to identify the comment and skip it to get the next line..

Out:
{test : test...test }

If the same comment consists of any space or any character in the place of *, my match fails..

IN:
{** *****    or   {* abccd   or {*abce

All the above cases are failed to skip ... What might be worng in the match,...can any one help me out..

3 Answers 3

1

The $ in your regex is an anchor that denotes the end of the string/line, which insists that the match will happen only when you have a row full of *s after the initial {

Removing this anchor will allow all three cases to match:

for ( '{*********', '{** *****', ,'{* abccd' , '{*abce' ) {

    print "$_ Matches\n" if /^{\*+/;
}
Sign up to request clarification or add additional context in comments.

2 Comments

i tried to remove the anchor but i do get an error...saying.. "Use of uninitialized value in concatenation (.) or string"
@NEW : Is the line number corresponding to the regex match, or is it another line?
0

It's unclear exactly what the syntax for comments is in your text, but given the examples, I'm going to assume that {* at the beginning of a line, followed by anything until the end of the line, is a comment.

In that case, your regex /(^{\*+$)/ won't work. That says:

  1. ^ - Match the beginning of the line
  2. { - Match a {
  3. \*+ - Match one or more *s
  4. $ - Match the end of the line

This pattern won't match if anything else comes between the *s and the end of the line.

Something like /^{\*.+$/ should do what you want. That changes your pattern to match one or more non-newline characters (the .) until you reach the end of the line.

2 Comments

i tried using the same but i do get an error...saying.. "Use of uninitialized value in concatenation (.) or string"
That warning (not error) is unrelated to the particular regex you might be using. Figure out what value is undefined and define it.
0

If you want to allow some other characters behind {* part, then just remove $ from the pattern.

This is a simple Perl code that you can start with:

@data = <main::DATA>;
print map { $_ if !/^{\*+/ } @data;

__DATA__
{****************************
{test : test...test}

Output:

{test : test...test}

Test this code here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.