• C

# What is the Regular Expression

Hello All,

What is the regular expression for following content

library <somelibrary>;
use <somelibrary>;

I am using POSIX regular expression library

Thanks Sudhakar
LVL 14
###### Who is Participating?

Commented:
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

Author Commented:
I mean if the strings comes some thing like

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

Commented:
if you are talking of only one space ...

library\s.*;

for many spaces

library\s*.*;

0

Author Commented:
it is white space means possibliity of tabs also
0

Author Commented:
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

Commented:
You mean:

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

(not sure, but that might work)
0

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

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

might work.
0

Commented:
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

Commented:
Try

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

Author Commented:
avizit,

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

for example

method test();
or

method test1(   );

0

Author Commented:
avizit,

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

for example

method test();
or

method test1(   );

0

Commented:
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

Author Commented:
ok,

same thing is working for this also
0

Author Commented:
one last question

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

0

Commented:
you should escape them with a backslash

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

Author Commented:
ok,

let me try

Thanks for continuous helping
0

Author Commented:
How to handle case insensitve strings??
0

Commented:
for POSIX regular expressions you can use

[:alpha:]  to denote alphanumeric charcters

0

Commented:
ops i mean

[:alpha:] for alphabetic

[:alnum:] for alpha numeric

[:digit:]  for digits

[:lower:] for lower case

[:upper] for upper case
0

Author Commented:

[: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

Commented:
so you use [:alpha:] for both
0

Commented:
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

Author Commented:
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

Commented:
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

Commented:
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

Author Commented:
thanks :-)
0

Commented:
The accepted answer looks rather loose to me ;-)
0

Commented:
*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

Commented:
Up to him, but of course it'll also match

library of donald duck;

;-)
0

Commented:
right :)  thanks
guess could have used [:alnum:] instead of  the "."

0

Commented:
... then that would just leave

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

;-)
0

Commented:
the alternation I understand  but what with the 'brackets' ?
0

Commented:
Maybe he didn't mean to match '<' and '>' ..?
0

Commented:
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

Commented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.