?
Solved

What is the Regular Expression

Posted on 2004-08-22
35
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 12
  • 6
  • +2
35 Comments
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11867423
I mean if the strings comes some thing like

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

Expert Comment

by:avizit
ID: 11867439
if you are talking of only one space ...

library\s.*;  

for many spaces

library\s*.*;


0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11867444
it is white space means possibliity of tabs also
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11867475
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
ID: 11867478
You mean:

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

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

Expert Comment

by:Zyloch
ID: 11867485
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
ID: 11867489
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 2000 total points
ID: 11867491
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
ID: 11867666
Try

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

Author Comment

by:sudhakar_koundinya
ID: 11868180
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
ID: 11868199
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
ID: 11868206
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
ID: 11868215
ok,

same thing is working for this also
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11868227
one last question

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

0
 
LVL 11

Expert Comment

by:avizit
ID: 11868236
you should escape them with a backslash

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

Author Comment

by:sudhakar_koundinya
ID: 11868249
ok,

let me try


Thanks for continuous helping
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11868506
How to handle case insensitve strings??
0
 
LVL 11

Expert Comment

by:avizit
ID: 11868859
for POSIX regular expressions you can use


[:alpha:]  to denote alphanumeric charcters

0
 
LVL 11

Expert Comment

by:avizit
ID: 11868865
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
ID: 11868926

[: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
ID: 11868943
so you use [:alpha:] for both
0
 
LVL 11

Expert Comment

by:avizit
ID: 11868955
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
ID: 11868963
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
ID: 11869026
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
ID: 11869031
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
ID: 11869116
thanks :-)
0
 
LVL 86

Expert Comment

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

Expert Comment

by:avizit
ID: 11872491
*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
ID: 11872544
Up to him, but of course it'll also match

library of donald duck;

;-)
0
 
LVL 11

Expert Comment

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


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11872663
... 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
ID: 11872704
the alternation I understand  but what with the 'brackets' ?
0
 
LVL 86

Expert Comment

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

Expert Comment

by:avizit
ID: 11872744
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
ID: 11872773
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

719 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