Home arrow static arrow Java Programming [Archive] - Need help creating a regular expression
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Need help creating a regular expression
This topic has 12 replies on 1 page.

Posts:70
Registered: 11/5/02
Need help creating a regular expression  
Aug 3, 2004 1:48 PM



 
I have a string containing the following...

/f1/folder1/folder2/folder3/doc/companies/Yahoo/file.php
/f1/folder1/folder2/folder3/doc/companies/MSN/logo-ahoo.gif
/f1/folder1/folder2/folder3/doc/companies/Excite/afile.php
/f1/folder1/folder2/folder3/doc/proditem/CompanyA/afile.php

There will be hundreds (possibly) entries like the above read from a file. I need to write a regular expression which just returns the folder immediately after either the proditem folder or companies folder. Thats it. So from a regular expression i should get back

Yahoo
MSN
Excite
CompanyA

What would my reg exp look like? I tried before but ended up in a mess.

Thanks in advance!

 

Posts:2,206
Registered: 8/15/02
Re: Need help creating a regular expression  
Aug 3, 2004 1:57 PM (reply 1 of 12)



 
Do you have a guaranteed directory structure where
(a) there are no subdirectories under, say, Yahoo?
(b) the company name is always under a directory /f1/folder1/folder2/folder3/doc/xxxxx?

If (a) us File.getParent() and String.subString() with String.lastIndexOf()
If (b) then use String.split() and get the 5th element of the array (***non portable to Windoze***)

If (a) and (b) use the method for (a) since it is portable

No need for regex
 

Posts:2,206
Registered: 8/15/02
Re: Need help creating a regular expression  
Aug 3, 2004 2:14 PM (reply 2 of 12)



 
And cos I am bored
public class Test {                                                                                                           public String getParentName( String name ) {        File file = new File (name );        String parent = file.getParent();        String company = parent.substring( parent.lastIndexOf("/")+1, parent.length() );        return company;    }                                                                                                           public String getNameByElement ( String name ) {        String [] names = name.split("\\/");        return names[3];    }                                                                                                                                                                                                                  public static void main( String [] args ) {        Test t = new Test();        try {            String s = "/tmp/test/Google/foo";            System.out.println( "Company name from Parent = " + t.getParentName(s) );            System.out.println( "Company name be element = " + t.getNameByElement(s) );        }        catch (Exception e) {            e.printStackTrace();        }    }}
 

Posts:3,258
Registered: 00-08-28
Re: Need help creating a regular expression  
Aug 3, 2004 2:22 PM (reply 3 of 12)



 
And cos I am bored [code]

And is it an excuse?
 

Posts:13,250
Registered: 24/10/97
Re: Need help creating a regular expression  
Aug 3, 2004 2:41 PM (reply 4 of 12)



 
import java.util.regex.*; public class Test20040803a{    private static final String[] fileValues =    {        "/f1/folder1/folder2/folder3/doc/companies/Yahoo/file.php",        "/f1/folder1/folder2/folder3/doc/companies/MSN/logo-ahoo.gif",        "/f1/folder1/folder2/folder3/doc/companies/Excite/afile.php",        "/f1/folder1/folder2/folder3/doc/proditem/CompanyA/afile.php",    };        private static Pattern pattern = Pattern.compile(".*/([^/]*)/[^/]*$");        public static void main(String[] args)    {        for (int index = 0; index < fileValues.length; index++)        {            String line = fileValues[index];            Matcher matcher = pattern.matcher(line);            if (matcher.matches())            {                System.out.println(matcher.group(1));            }        }         }}
 

Posts:2,206
Registered: 8/15/02
Re: Need help creating a regular expression  
Aug 3, 2004 2:52 PM (reply 5 of 12)



 
which doesn't work with wiki/.//f1/folder1/folder2/folder3/doc/companies/MSN/foobar/logo-ahoo.gif - hence my original questions!
 

Posts:13,250
Registered: 24/10/97
Re: Need help creating a regular expression  
Aug 3, 2004 3:00 PM (reply 6 of 12)



 
which doesn't work with
/f1/folder1/folder2/folder3/doc/companies/MSN/foobar/lo
o-ahoo.gif - hence my original questions!
You are right! I mis-read the question!
 

Posts:316
Registered: 4/7/04
Re: Need help creating a regular expression  
Aug 3, 2004 3:01 PM (reply 7 of 12)



 
If (b) then use String.split() and get the 5th element
of the array (***non portable to Windoze***)

If (a) and (b) use the method for (a) since it is
portable

You can use:

System.getProperty("file.separator")
 

Posts:13,250
Registered: 24/10/97
Re: Need help creating a regular expression  
Aug 3, 2004 3:06 PM (reply 8 of 12)



 
If at first you don't succeed ...

'EvilEdnas' questions still need to be addressed but this gives the correct result for the sample values.

import java.util.regex.*; public class Test20040803a{    private static final String[] fileValues =    {        "/f1/folder1/folder2/folder3/doc/companies/Yahoo/file.php",        "/f1/folder1/folder2/folder3/doc/companies/MSN/logo-ahoo.gif",        "/f1/folder1/folder2/folder3/doc/companies/Excite/afile.php",        "/f1/folder1/folder2/folder3/doc/proditem/CompanyA/afile.php",        "/f1/folder1/folder2/folder3/doc/companies/MSN/foobar/logo-ahoo.gif",    };        private static Pattern pattern = Pattern.compile(".*/(?:companies|proditem)/([^/]*)/.*$");        public static void main(String[] args)    {        for (int index = 0; index < fileValues.length; index++)        {            String line = fileValues[index];            Matcher matcher = pattern.matcher(line);            if (matcher.matches())            {                System.out.println(matcher.group(1));            }        }         }}
 

Posts:2,206
Registered: 8/15/02
Re: Need help creating a regular expression  
Aug 3, 2004 3:10 PM (reply 9 of 12)



 
I really wish I could do this regex ju-ju!
 

Posts:13,250
Registered: 24/10/97
Re: Need help creating a regular expression  
Aug 3, 2004 3:15 PM (reply 10 of 12)



 
I really wish I could do this regex ju-ju!
Having read many of your posts I think you would pick it up in no time!


 

Posts:10,967
Registered: 4/7/01
Re: Need help creating a regular expression  
Aug 3, 2004 4:22 PM (reply 11 of 12)



 
Another one...
(probably quite a bit faster than a regular expression solution)
public class zz{    public static void main(String[] args)    {        int m;        int n;        int x;        int y;                String[] st =            {"/f1/folder1/folder2/folder3/doc/companies/Yahoo/file.php",            "/f1/folder1/folder2/folder3/doc/companies/MSN/logo-ahoo.gif",            "/f1/folder1/folder2/folder3/doc/companies/Excite/afile.php",            "/f1/folder1/folder2/folder3/doc/proditem/CompanyA/afile.php"};         for (x = 0; x < st.length; x++)        {            m = (n = st[x].indexOf("proditem")) == -1 ?                st[x].indexOf("companies") : n;             String out = st[x].substring                (y = st[x].indexOf('/', m) + 1, st[x].indexOf('/', y));             System.out.println(out);        }    }}prints YahooMSNExciteCompanyA
 

Posts:70
Registered: 11/5/02
Re: Need help creating a regular expression  
Aug 5, 2004 5:53 AM (reply 12 of 12)



 
(a) there are no subdirectories under, say, Yahoo?

There can be directories under yahoo

(b) the company name is always under a directory
/f1/folder1/folder2/folder3/doc/xxxxx?

Yes that is correct.

Thanks for all your suggestions. I will try them all out and use whichever is quickest. Thanks!!!
 
This topic has 12 replies on 1 page.