Regular Expression in Perl - very challenge


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regular Expression in Perl - very challenge
# 1  
Old 03-08-2011
Regular Expression in Perl - very challenge

Hi expert,

As we know in regular expression, [^ab] means neither a nor b.

Now the question is how to consider <ab> as a whole part or somehow a single char. Something like [^(ab)] which is meaning neither ( nor a nor b nor ).

Or do we have another way for this?

The real requirement is as below:
Code:
find the number of semicolon<;>, which should not inside /* and */.

example1 :
Code:
a;b;c           --  2
a;/*;;*/g;c  --  2
a/;a;a;/b     --  3
a*;a;a;/b     --  3
a*;a;a;*b     --  3

I know below expression to count the number of semicolon which is inside / pair or * pair

my @cnt = $str =~ /;
(?=
(?:[^\/]*$) (?# there is no \/ after ; )
|
(?: (?:[^\/]*?\/[^\/]*\/[^\/]*)*$) (?# if have \/ after ;, must appear in pair)
)/gx;

Here i am struggling with the red highlighted part, which means not </>, how can we make it somehow mean not </*>?


Please help. Thanks in advanced

Last edited by summer_cherry; 03-08-2011 at 06:21 AM..
# 2  
Old 03-08-2011
Would this work..to mean no ab's together?
Code:
[^a][^b]

And in unix regexp (not sure with Perl)we do not need to escape special characters when given within square brackets.Please remove those escape characters present within square brackets and try. I also did not understand where you struggle, can you elaborate?

---------- Post updated at 04:43 PM ---------- Previous update was at 04:15 PM ----------

Try the below reqexp using save-part. It matches anything in between, including /*..*/
Code:
\(\/\)\(\*\).*\2\1
echo 'a;/*;;*/g;c' |sed 's/\(\/\)\(\*\).*\2\1//'

# 3  
Old 03-08-2011
Something like this:
Code:
# cat file
a;b;c           --  2
a;/*;;*/g;c  --  2
a/;a;a;/b     --  3
a*;a;a;/b     --  3
a*;a;a;*b     --  3

Code:
#  perl -ane 's/\/\*(([^\/][^\*])*)\*\///;$n=(scalar(split/;/)-1);print $n."\n"' file
2
2
3
3
3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl regular expression

Hi , I have the below array my @actionText = ("delivered to governor on 21/23/3345" , "deliver jllj" , "ram 2345/43"); When i am trying to grep the contents of array and if mathced substituting with the digitis or some date format from the element like below my @action = grep { $_ =~... (7 Replies)
Discussion started by: ragilla
7 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

3. Shell Programming and Scripting

Perl regular expression help!

Hi I am doing something basic like... if ($stringvariable =~ /have not typed/) I have a little problem because the 'not' in the expression gets highlighted as a kind of a '!'..what am I supposed to do in this situation? Thank you ---------- Post updated at 03:24 PM ----------... (1 Reply)
Discussion started by: vas28r13
1 Replies

4. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

5. Shell Programming and Scripting

Perl regular expression and %

Could you help me with this please. This regular expression seems to match for the wrong input #!/usr/bin/perl my $inputtext = "W1a$%XXX"; if($inputtext =~ m/+X+/) { print "matches\n"; } The problem seems to be %. if inputtext is W1a$XXX, the regex doesnot match.... (5 Replies)
Discussion started by: suppandi7
5 Replies

6. Shell Programming and Scripting

Perl Regular Expression

Hello, I am trying to use perl LWP module to read and get a specfic URL page. The issue is that the URL ends with the data and time and time is not consistent it changes all the time. if anyone could help me how to write a regular expressin that would work in the LWP::UserAgent get function to... (0 Replies)
Discussion started by: bataf
0 Replies

7. Shell Programming and Scripting

Regular expression in Perl

Hi, I need and expression for a word like abc_xyz_ykklm The expresion should indicate that the word starts with abc and end with ykklm but does not contain xyz string in the middle. Example: abc_tmn_ykklm is ok and abc_xyz_ykklm is not Ok. Please help. Regards. (1 Reply)
Discussion started by: asth
1 Replies

8. Shell Programming and Scripting

PERL regular expression

Hello all, I need to match the red expressions in the following lines : MACRO_P+P-_scrambledServices_REM_PRC30.xml MACRO_P+P-_scrambledServices_REM_RS636.xml MACRO_P+P-_scrambledServices_REM_RS535.xml and so on... Can anyone give me a PERL regular expression to match those characters ? ... (5 Replies)
Discussion started by: lsaas
5 Replies

9. Shell Programming and Scripting

regular expression in perl

hi, i want to extract the sessionID from this line. QnA Session Id : here the output should be-- QnA_SessionID=128589 Thanks NT (3 Replies)
Discussion started by: namishtiwari
3 Replies

10. Shell Programming and Scripting

perl regular expression

letz say that my file has 7 records with only one field. So my file has: 11111111 000000000000000 1111 aaaabbbccc 1111111222000000 aaaaaaaa zz All i need is: 1. when the field has a repetition of the same instance(a-z or 0-9), i would consideer it to be invalid.... (1 Reply)
Discussion started by: helengoldman
1 Replies
Login or Register to Ask a Question