Solved

What is the Regular Expression

Posted on 2004-08-22
35
320 Views
Last Modified: 2010-04-15
Hello All,

What is the regular expression for following content

library <somelibrary>;
use <somelibrary>;


I am using POSIX regular expression library

Thanks Sudhakar
0
Comment
Question by:sudhakar_koundinya
  • 13
  • 12
  • 6
  • +2
35 Comments
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
I mean if the strings comes some thing like

String <SPACE> <SOME String> <Semi colon>      
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
if you are talking of only one space ...

library\s.*;  

for many spaces

library\s*.*;


0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
it is white space means possibliity of tabs also
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
any way let me try for both with that regular expression

String <SPACE> <SOME String> <open bracket> <possiblity of space> <close bracket> <Semi colon>    

If this is the situation

will this expression works??

library\s.*(\s.);  
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
You mean:

^\S+(\s|\t)+\S;$

(not sure, but that might work)
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
For your String <SPACE> <SOME String> <open bracket> <possiblity of space> <close bracket> <Semi colon>, this should work:

library\s.*?\(\s*\);

might work.
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
library\s.*\(\s*\); probably should work also, I'm not sure why your example above had a ".", because if you have a ".", it means you can have anything in the bracket, even digits, etc. so you would just need:

library\s.*(.*);  
0
 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
Comment Utility
you can also use

library[[:space:]]+.+;

sorry those should have been '+' instead of '*' in the earlier reply

check http://www.dc.turkuamk.fi/docs/gnu/rx/rx_3.html
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Try

library|use\s+<\w+>;
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
avizit,

your last expression has worked for me. But what is expression if it contains ()

for example

method test();
or

method test1(   );            


0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
avizit,

your last expression has worked for me. But what is expression if it contains ()

for example

method test();
or

method test1(   );            


0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
that should depend on if you are going to have something between the braces or its going to be empty

method[[:space:]]+.+\([[:space:]]+\);


i.e you may have to escape the ( )'s

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
ok,

same thing is working for this also
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
one last question

How to handle if the sting contains % and/or *

0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
you should escape them with a backslash

use \%  , \* for a literal % and *
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
ok,

let me try


Thanks for continuous helping
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
How to handle case insensitve strings??
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

Expert Comment

by:avizit
Comment Utility
for POSIX regular expressions you can use


[:alpha:]  to denote alphanumeric charcters

0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
ops i mean

[:alpha:] for alphabetic

[:alnum:] for alpha numeric


[:digit:]  for digits

[:lower:] for lower case

[:upper] for upper case
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility

[:lower:] for lower case

[:upper] for upper case

checks explicitly for lower case or upper case. I need to have a mixed case.

Any other Ideas

Thanks
Sudhakar
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
so you use [:alpha:] for both
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
and [:alnum:] for alphanumeric ie

[a-zA-Z0-9]  you can use this too instead of the above  for alphanumeric

for only alphabetic of both cases you can use

[a-zA-Z]
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
Hmm,



if  I have some thing like this

Library test;
or
library test;
or
LIBRARY test;

then
library[[:space:]]+.+; will work for second case only






0
 
LVL 22

Expert Comment

by:grg99
Comment Utility
Regular expressions are NFG for parsing perl syntax, since a #comment can appear anywhere.  Or a \escape.

Perl is just too loose a syntax to be parsed by anything as regimented as regular expressions.

0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
oh that you you can use

(library|LIBRARY)  if you want to match only those two forms

you can also you

[lL][iI][bB][rR][aA][rR][yY]  
if you want to match  
library
Library
LIbrary .. and so on

yhou might be able to use  /library/i    i.e the i switch which makes it case insenstive .. not sure if POSIX regular expressions allows it or not
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
thanks :-)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
The accepted answer looks rather loose to me ;-)
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
*shrug* .:)
but if  any of the above were wrong or incorrect , i guess you can always point it out.  Sudhakar can always unaccept  the question. I mean that's better than having a wrong solution.


/abhijit/
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Up to him, but of course it'll also match

library of donald duck;

;-)
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
right :)  thanks
guess could have used [:alnum:] instead of  the "."


0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
... then that would just leave

a. the left angle bracket
b. the right angle bracket
c. the alternation between 'use' and 'library'

;-)
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
the alternation I understand  but what with the 'brackets' ?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Maybe he didn't mean to match '<' and '>' ..?
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
oh okay then i can't help atleats on this .. i can only do what is asked :)

BUt thanks nonetheless for pointing out the error :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now