Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Need help creating regexp

Last post 03-01-2010, 6:27 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  03-01-2010, 4:18 AM 60209

    Need help creating regexp

    Hi.

    I need construction advice on the following (for java and windows filesystem):

    #1: Convert full java class name to filename:
      Example:  com.example.ClassName      -> com/example/ClassName.java
             :  com.example.ClassName.java -> com/example/ClassName.java

    #2: Convert image in classpath to filename (file extension may vary):
      Example:  com.example.images.Image.png -> com/example/images/Image.png

    #3: Add path and wildcard to a filepath (if it does not start with "XXXX." where XXXX can be any characters upper and lower case) :
      Example: p_databasePackage.pkb       -> *.p_databasePackage.pkb
             : CHDEV.p_databasePackage.pkb -> CHDEV.p_databasePackage.pkb

    The original values are passed as a list of filetype + filename from a web app, and I need to validate their existance in a SCM system. The filetypes and how to resolve their path is stored in a database. I would really like to include a regexp per filetype on further resolving the filename, since users do not enter filenames as such.

    I would really appreciate all and any help given on this.

  •  03-01-2010, 6:27 PM 60231 in reply to 60209

    Re: Need help creating regexp

    I would approach these as follows:

    For the case where the ".java" is missing, use the normal string matching functions to see if the suffix is ".java" - if not then append the required string. While it may be possible to come up with a regex, it will be very complex as detecting the absence of a prefix is messy (to say the least). The best you can do is to use a regex to find the ".java" suffix and then use your programming language to append what you want if the match fails - but you can do all this with simple string operations far more easily.

    To change all but the last "." to "/", try the pattern:

    \.(?=.*?\.)

    and the replacement string

    /

    This looks for a literal period that is followed by another one and replaces it with a "/". The last period will not be followed by another one and so will not be replaced. This covers the 2nd case of example #1 as well as example #2.

    For example #3 (I assume that the generic match is to present "*." if there is only a single period in the string) use the pattern:

    ^(\w+\.\w+)$

    and replacement string of

    *.$1

    (You may need to use '\1' or something else to refer to the captured text form match group #1 in the replacement string). If the composition of your filename induces characters that are not in the '\w' class (such as "$" and the like) then you will need to alter these parts.

    Each of these regex replacements will need a separate pass as you cannot tell during the replacement part which replacement string to use.

    Susan

View as RSS news feed in XML