Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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
0
sudhakar_koundinya
Asked:
sudhakar_koundinya
  • 13
  • 12
  • 6
  • +2
1 Solution
 
sudhakar_koundinyaAuthor Commented:
I mean if the strings comes some thing like

String <SPACE> <SOME String> <Semi colon>      
0
 
avizitCommented:
if you are talking of only one space ...

library\s.*;  

for many spaces

library\s*.*;


0
 
sudhakar_koundinyaAuthor Commented:
it is white space means possibliity of tabs also
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
sudhakar_koundinyaAuthor 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
 
ZylochCommented:
You mean:

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

(not sure, but that might work)
0
 
ZylochCommented:
For your String <SPACE> <SOME String> <open bracket> <possiblity of space> <close bracket> <Semi colon>, this should work:

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

might work.
0
 
ZylochCommented:
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
 
avizitCommented:
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
 
CEHJCommented:
Try

library|use\s+<\w+>;
0
 
sudhakar_koundinyaAuthor Commented:
avizit,

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

for example

method test();
or

method test1(   );            


0
 
sudhakar_koundinyaAuthor Commented:
avizit,

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

for example

method test();
or

method test1(   );            


0
 
avizitCommented:
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
 
sudhakar_koundinyaAuthor Commented:
ok,

same thing is working for this also
0
 
sudhakar_koundinyaAuthor Commented:
one last question

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

0
 
avizitCommented:
you should escape them with a backslash

use \%  , \* for a literal % and *
0
 
sudhakar_koundinyaAuthor Commented:
ok,

let me try


Thanks for continuous helping
0
 
sudhakar_koundinyaAuthor Commented:
How to handle case insensitve strings??
0
 
avizitCommented:
for POSIX regular expressions you can use


[:alpha:]  to denote alphanumeric charcters

0
 
avizitCommented:
ops i mean

[:alpha:] for alphabetic

[:alnum:] for alpha numeric


[:digit:]  for digits

[:lower:] for lower case

[:upper] for upper case
0
 
sudhakar_koundinyaAuthor 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
 
avizitCommented:
so you use [:alpha:] for both
0
 
avizitCommented:
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
 
sudhakar_koundinyaAuthor 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
 
grg99Commented:
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
 
avizitCommented:
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
 
sudhakar_koundinyaAuthor Commented:
thanks :-)
0
 
CEHJCommented:
The accepted answer looks rather loose to me ;-)
0
 
avizitCommented:
*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
 
CEHJCommented:
Up to him, but of course it'll also match

library of donald duck;

;-)
0
 
avizitCommented:
right :)  thanks
guess could have used [:alnum:] instead of  the "."


0
 
CEHJCommented:
... then that would just leave

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

;-)
0
 
avizitCommented:
the alternation I understand  but what with the 'brackets' ?
0
 
CEHJCommented:
Maybe he didn't mean to match '<' and '>' ..?
0
 
avizitCommented:
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
 
CEHJCommented:
:-)
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.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

  • 13
  • 12
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now