Parse string based on pattern

I'm trying to make a small prog that extracts a song's artist from its filename based on a specified pattern.
Something like this:
public String getArtist(String filename, string pattern) {
String artist = getArtist("The artist - the title.mp3", "artist - title");
artist = getArtist("01 - Another title - Another artist.mp3", "track - title - artist");Any good ideas on how to implement getArtist would be appreciated... :)

heres my idea of how to solve it...
this solution is not complete, but you may perfect it any way you like... it's just a quick and dirty hack ;)
import java.util.regex.*;
public class ArtistProblem {
public static void main(String[] args) {
  if (args.length == 2) {
   System.out.println(getArtist(args[0], args[1]));
  } else {
   System.out.println(getArtist("The artist - the title.mp3", "artist - title"));
   System.out.println(getArtist("01 - Another title - Another artist.mp3", "track - title - artist"));
public static String getArtist(String filename, String pattern) {
  // change track to regex that matces any spaces nad word characters
  pattern = pattern.replaceAll("track", "[\\\\w ]*");
  // change title to regex that matces any spaces nad word characters
  pattern = pattern.replaceAll("title", "[\\\\w ]*");
  // remove extention from the end of filename
  filename = filename.replaceAll("\\.mp3$", "");
  // change artist to regex that groups matched pattern
  pattern = pattern.replaceAll("artist", "([\\\\w ]*)");
  Pattern patt = Pattern.compile(pattern);
  Matcher matcher = patt.matcher(filename);
  if (matcher.find()) {
   return matcher.group(1);
  } else {
   // no match was found, so we return empty string
   return "";
}try running it so:
java ArtistProblem "the artist - the track - the title.mp3" "artist - track - title"
and you'll get:
the artist
i hope that gives you a jumpstart to some direction.

Similar Messages

  • String parser question based on oracle grammer

    If i have a string like :
    update employee
    set wtKey = name||pno||id
    where id is not null
    Then my API should return this when i pass "||"
    name||pno||id
    I tried doing it using ' ' as dilimiter and check to see if the parsed string has || but that won't work when the above update cmd is written in the following manner:
    update employee
    set wtKey = name || pno || id
    where id is not null
    (or)
    update employee
    set wtKey = name|| ' ' ||id
    where id is not null
    Is there any API or third party stuff that can perform such operation instead of we parsing it based on some assumptions.
    Any help would be appreciated

    Matt,
    In simple terms, whenever you change some thing, its needed to be logged for the recovery. So when we change Oracle's datablock's data, we are creating one entry. The change has an undo entry also related to it, this would go to the Undo block. Now 'go' means we are updating undo block's current image(whatever it may be) with our current undo image for our transaction, a change hence has to be logged, second redo entry. Transaction table again has to be updated to maintain the entries of the current transaction, some records over there are updated, hence redo.
    The basic thing is that Oracle would make sure that we have all the changes, where ever they are done, logged in the log files for the crash. In case we wont have it, we wouldn't be able to perform recovery.
    A very simplified explanation :-).
    Aman....

  • How to extract substring from a string based on the condition ??

    Hi,
    I'm having a very large string which as below
    EQD+CN+SAMPLE18767+2200+++5'
    NAD+CA+FIR:172:20'
    DGS+IMD+3.2+2346+55:CEL'
    FTX+AAA+++GOOD'
    FTX+AAA+++ONE'
    EQD+CN+SAMPLE18795+2200+++5'
    NAD+CA+TIR:172:20'
    DGS+IMD+3.2+2346+55:CEL'
    FTX+AAA+++SECOND'
    FTX+AAA+++IS FAIR'
    similarly FTX+AAA as above and it goes on
    i tokenized each segment with delimiter as ' and able to read each segment.
    Now i want to concatenate the FTX+AAA in a single segment if more than one FTX+AAA with IMMEDIATE below
    The output is as follows
    EQD+CN+SAMPLE18767+2200+++5'
    NAD+CA+FIR:172:20'
    DGS+IMD+3.2+2346+55:CEL'
    FTX+AAA+++GOOD,ONE'
    EQD+CN+SAMPLE18795+2200+++5'
    NAD+CA+TIR:172:20'
    DGS+IMD+3.2+2346+55:CEL'
    FTX+AAA+++SECOND,IS FAIR'
    similarly FTX+AAA should be concatenated if it has similar FTX+AAA IMMEDIATE below.
    The FTX+AAA segments can come any number of times immediate below
    Please help me how we can do this??? Can anyone help me with the code snippet to do this?
    Thanks,
    Kathir

    Encephalopathic wrote:
    You've posted > 300 times here and you still don't respect the rule regarding notification of all cross-posts? [http://www.java-forums.org/advanced-java/30061-how-extract-substring-string-based-condition.html]
    Do you think this this will help convince others to help you?See also [http://www.coderanch.com/t/500088/java/java/extract-substring-string-based-condition|http://www.coderanch.com/t/500088/java/java/extract-substring-string-based-condition].

  • Spliting a string based on Non Printable character

    Hi,
    i have a requirement where i have to split a String based in non printable character " MYU(ascii: 230);
    es: ""This is to test æ raaaaaaaaaaaaaaaaa æ AAA010224544 æ 7118288888 æ
    æ is a not printable character and its ascci is 230..
    iam getting that string from form how to split it in Java..
    Any suggestions...?

    One of many ways
        String initString = "This is to test æ raaaaaaaaaaaaaaaaa æ AAA010224544 æ 7118288888 æ";
        String[] tokens = initString.split(String.valueOf((char)230));
        for (String string : tokens)
          System.out.println(string.trim());
        }

  • Parsing String

    i have problem to parse string to document
    i have string like this str = "<root><data>1</data><data>2</data><root>";
    how to parse this string in docoment xml

    import java.io.*;
    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.output.*;
    public class PrettyPrinter {
       public static void main(String[] args) {
            // Assume filename argument
            String filename = args[0];
            try {
                // Build the document with SAX and Xerces, no validation
                SAXBuilder builder = new SAXBuilder();
                // Create the document
                Document doc = builder.build(new File(filename));
                // Output the document, use standard formatter
                XMLOutputter fmt = new XMLOutputter();
                fmt.output(doc, System.out);
            } catch (Exception e) {
                e.printStackTrace();
    }The above code is taken directly from
    http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom-p2.html
    u don't have to use Factories, this is what is said in that excerpt, it is very easy to use, and it is said that 80 % of the xml work can be done with 20 % or less work using JDOM.
    n joy ....
    </ksenji>

  • How to parse string to date in j2me?

    How to parse string to date in j2me?
    Please help me.

    Hi..
    String dyStr = "20";
    String mtStr = "1";
    String yrStr = "1999";
    Calendar cal = Calendar.getIntstance();
    int dy = Integer.parseInt(dyStr);
    int mt = Integer.parseInt(mtStr);
    int yr = Integer.parseInt(yrStr);
    cal.set(Calendar.DATE, dy);
    cal.set(Calendar.MONTH, mt);
    cal.set(Calendar.YEAR, yr);
    OK?

  • Check string validity with pattern

    hello,
    I want to check the validity of strings given by the user. The only characters authorized are : 'a' to 'z', 'A' to 'Z', '0' to '9', '.', '-', '_', '*', '@' and ' ' (space)
    i want to check this string with a pattern but it does not work.
    Somebody can help me for the pattern because the API javadoc is poor and i have a limited web access in my agency.
    Thanks
       String regex = "[a-zA-Z0-9.*-_@]";
       System.out.println(regex);
       boolean bol = Pattern.matches(regex, field);
       System.out.println(bol);
       ->> result always false for string field="Indy jones";

    try this:
    import java.sql.*;
    import java.io.*;
    import java.util.regex.*;
    import java.util.*;
    public class RegExTest2 {
    public static void main (String args[]) {
    Pattern pat = null;
    Matcher m = null;
    String patternToMatch = "^[a-zA-Z0-9\\.\\*\\-\\_@\\s]+$";
    String line1 = "IndianaJones";
    String line2 = "Indiana Jones";
    String line3 = "Indiana - Jones";
    String line4 = "Indiana&Jones";
    String line5 = "Indiana & Jones";
    String line6 = "  Indiana Jones  ";
    String line7 = "Indiana Jones =";
    pat = Pattern.compile(patternToMatch);
    System.out.println("Pattern to match = " + patternToMatch);
    boolean bol = Pattern.matches(patternToMatch, line1);  
    System.out.println("line1 : expected true and got " + bol);
    bol = Pattern.matches(patternToMatch, line2);  
    System.out.println("line2 : expected true and got " + bol);
    bol = Pattern.matches(patternToMatch, line3);  
    System.out.println("line3 : expected true and got " + bol);
    bol = Pattern.matches(patternToMatch, line4);  
    System.out.println("line4 : expected false and got " + bol);
    bol = Pattern.matches(patternToMatch, line5);  
    System.out.println("line5 : expected false and got " + bol);
    bol = Pattern.matches(patternToMatch, line6);  
    System.out.println("line6 : expected true and got " + bol);
    bol = Pattern.matches(patternToMatch, line7);  
    System.out.println("line7 : expected false and got " + bol);
      } // end main
    } //End Class RegExTest2

  • How could i parse string and link its model with my files in eclipse project?

    How could i parse string and link its model with my files in eclipse project?, as i read that we dont have to use standalone mode while working with eclipse projects.
    Example of what i want to do:
    I have file1.dsl in my project which contains some statements but the declaration of these statements are not in another file, i need to put them only in my code

    Hi Stefan,
    I have eclipse project contains 2 files, file1.dsl and file2.dsl, you can see the contents of the files below.
    file1.dsl contains some statements as shown below and file2.dsl contains the declarations of the variables. (like in C++ or Java we have declarations and usage)
    At this step file1.dsl and file2.dsl will be parsed successfully without any errors.
    Lets imagine that we will delete this project and will create another one and the new project will contain only file1.dsl (which contains the usage)
    At this step this file (file1.dsl) will contains some errors after parsing it, because we are using variables without declarations.
    So what i need is to parse the content of file2.dsl directly without adding this file to the project.
    I need to add the content of file2.dsl directly as a string in the code and parse it. just like that ( "int a;int b;" )
    And link file1.dsl with the model generated after parsing my string
    file1.dsl
    a++;
    b++;
    file2.dsl
    int a;
    int b;
    Thanks

  • Validate String with a pattern

    How can we validate the string with a pattern?
    eg. Value should always be of the format C-DDD,
    where C is a character (A-Z or a-z) and
              D represents a digit(0-9)
    Valid forms are A-001, A-987, Z-098 and
    not valid forms are A-01, 1-A01, 1- 0A0, A-A01, A-0001
    Say internal table contains values like this.
    F1     F2      F3
    1     00001  A-001
    2     00001  A-A01
    3     00001  B-909
    4     00001  Z-01
    5     00001  k-0001
    Valid records are 1 and 3.

    Hi,
    you can use regular expressions for this, e.g.:
      FIND ALL OCCURRENCES OF REGEX '[a-z,A-Z]-[0-9]{3}[ [:blank:] ]'
           IN TABLE t_data
           RESPECTING CASE
           RESULTS t_results.
    Note that in the above example all fields in the table will be checked. If this is not practical for your use you can use a LOOP and FIND on the table field.
    Note that line 5 in your example also matches the pattern you have given. If you really do not want to see this as valid you will have to use the pattern: '[A-Z]-[0-9][ [:blank:] ]'. The addition of [ [:blank:] ] is only needed if your field is longer than the pattern, i.e. 5 characters.
    Good luck,
    Gert.
    Edit: This will also be pretty efficient.
    Edited by: Gert Beukema on Jul 15, 2008 10:36 AM

  • Trouble parsing string

    I need to parse strings in the format "City State
    Abbreviation Zipcode" (ie "Glenview, IL 60062") to seperate them as
    their own variables. Problem is that sometimes there is a comma
    after city, sometimes not, so I've resorted to REfind to seperate
    the string. Here's the snippet, "city" is the entire string I
    mentioned above. The problem is that the refind I use seems to be
    returning 0. I'm trying to find the two capital letters that
    designate the state abbeviation.
    <cfif city neq ''>
    <cfset crpos = refind("[:upper:][:upper:]",city) >
    <cfset zip = trim(right(city,len(city)-crpos))>
    <cfset citystate = trim(left(city,crpos)) >
    <cfset newpos = find("#chr(32)#",citystate) >
    <cfset state =
    trim(right(citystate,len(citystate)-newpos)) >
    <cfset actualcity =
    trim(left(citystate,len(citystate)-newPos)) >
    </cfif>

    I probably should mention some explaination about what the
    regular expression is doing:
    Note: Groups are RegExp statements surrounded by ()
    Group 1: Combination of Letters and Spaces (e.g. City Name)
    optional comma and (required) space
    Group 2: 2 Character upper case state code (State Code) (note
    - depending on your source, state codes may not always be upper
    case)
    (required) space
    Group 3: 5 digit string (e.g. Zip Code) (note - again,
    depending on your source, you may be getting 5 digit zip + 4 or
    even non-us zip codes that may involve alpha characters.)
    The replace function is using back references to refer to the
    text matched by group 1,2 and 3.

  • Split Function unable to parse string correctly

    Hi,
    I'm using split function to split string into multiple rows using comma "," as delimiter. In a string I have following values which are not parsed correctly as they have comma inside the values. 
    American Dawn, Inc.
    Battalian USA, Inc.
    Fria USA, Inc.
    Lazer, Inc.
    Mexilink Inc.
    Is there any other approach to fix this issue?
    Here is the split function Im using:
    CREATE Function [dbo].[fnSplit] (
    @List varchar(MAX), 
    @Delimiter char(1)
    Returns @Temp1 Table (
    ItemId int Identity(1, 1) NOT NULL PRIMARY KEY , 
    Item varchar(MAX) NULL 
    As 
    Begin 
    Declare @item varchar(MAX), 
    @iPos int 
    Set @Delimiter = ISNULL(@Delimiter, ';' ) 
    Set @List = RTrim(LTrim(@List)) 
    -- check for final delimiter 
    If Right( @List, 1 ) <> @Delimiter -- append final
    delimiter 
    Select @List = @List + @Delimiter -- get position of
    first element 
    Select @iPos = Charindex( @Delimiter, @List, 1 ) 
    While @iPos > 0 
    Begin 
    -- get item 
    Select @item = LTrim( RTrim( Substring( @List, 1, @iPos
    -1 ) ) ) 
    If @@ERROR <> 0 Break -- remove item form list 
    Select @List = Substring( @List, @iPos + 1, Len(@List)
    - @iPos + 1 ) 
    If @@ERROR <> 0 Break -- insert item 
    Insert @Temp1 Values( @item ) If @@ERROR <> 0 Break 
    -- get position pf next item 
    Select @iPos = Charindex( @Delimiter, @List, 1 ) 
    If @@ERROR <> 0 Break 
    End 
    Return 
    End
    Another user in this forum posted a split function that
    he wrote:
    CREATE FUNCTION dbo.splitter(@string VARCHAR(MAX), @delim CHAR(1))
    RETURNS @result TABLE (id INT IDENTITY, value VARCHAR(MAX))
    AS
    BEGIN
    WHILE CHARINDEX(@delim,@string) > 0
    BEGIN
    INSERT INTO @result (value) VALUES (LEFT(@string,CHARINDEX(@delim,@string)-1))
    SET @string = RIGHT(@string,LEN(@string)-CHARINDEX(@delim,@string))
    END
    INSERT INTO @result (value) VALUES (@string)
    RETURN
    END
    GO
    Both of them are unable to parse above values incorrectly.
    FYI:  String is made of values that are selected
    by user in SSRS report. I think SSRS when combine values , put comma "," between multiple values.
    Any help or guidance would be appreciated.
    ZK

    duplicate of
    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/820ac53c-ce25-4cc7-b828-5875a21d459d/split-function-unable-to-parse-string-correctly-in-ssrs-report?forum=sqlreportingservices
    please dont cross post
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Hi to parse String "x;y:z" in Core Java

    hi to parse String "x;y:z" in Core Java

    Deepak_A_L wrote:
    how do i parse a String "X;Y:Z" in java i.e the results of parsing the string
    String s = "X;Y:Z"
    in terms of ENGLISH LANGUAGE -->(X Semicolon Y Colon Z)
    should be the below o/p individual Strings.
    X
    Y
    Z
    how do i get the above output.????Split on a semi- or regular colon using String's split(String regex) method.

  • Determining the actual length of a string based on pixels?

    How would you determine the actual length of a string based on pixels? Reason for is because a length of a string containing all " l " chars would be a lot smaller then the length of a string containing all "H" chars based on pixel width.
    thanks,
    newbie

    Yes, look at the FontMetrics class which has methods to do just that. To get a relevant FontMetrics object, try "x.getFontMetrics(f)" where x is a Component and f is the Font your string is represented in.

  • How to parse and isolate a pattern from a string?

    Hello friends --
    I am currently using the regex package to parse pathnames; i.e., active/99999999999/correspondence/file.tiff~23698, where 99999999999 is a variable 11-digit number. I have a method that is intended to not only parse but also isolate the 11-digit number and return it to the caller. The problem is that rather than return just the number, the method returns the entire string in which it is found.
    Perhaps I'm going about this the wrong way. Perhaps I should convert everything to a char or byte[], and spin through each element in the array. If a digit is found, append that digit into a new array and return the new array. Sounds like it would take forever to run.
    Does anyone have a clue?? A big, grateful thanks to anyone who can lend a hand.
    Here is the code:
    public static String locate(String subject, String find)
    Pattern p = Pattern.compile(find);
    Matcher m = p.matcher(subject);
    String found = null;
    if (m.find()) found = m.group(0);
    return found;

    I think your code should work, if you get the pattern right.
    Using pattern \d{11} with your code ("\\d{11}" as a string), I can extract the 11 digit number. But if the path contains two eleven-digit numbers, do you know for sure that you want the first one?
    What pattern are you using?
    HTH,
    Ken

  • Mastermind / Parse string with error handling ....

    Ok I am not going to hide the fact this looks like a homework question, honestly I already handed it in and now this is bothering me that I couldn't figure it out.......
    We had to make a mastermind client/server game only using text no graphical everything runs fine but how would one do error catching on this. (code posted below)
    Basically the user types in the command "guess r r r r" (guessing that the secret code is red four times ) and it parses it and assigns guess[] with the r and checks it based on the randomly generated color. The problem that I have is that is if someone makes a typo for example guess r rr r. How would one stop it from crashing from an out of bounds array request?
    if (command.matches("guess.*"))
                               int white = 0;
                               int black = 0;
                               String phrase = command;
                               String delims = "[ ]+";
                               String[] guess = { null, null, null, null, null, null, null };
                               guess = phrase.split(delims);
                               for (int i = 0; i < 4; i++)
                                    if (color.equalsIgnoreCase(guess[i+1]))
                             black++;
                        else if (color[i].equalsIgnoreCase(guess[i+1]))
                             white++;
                        else if (color[i].equalsIgnoreCase(guess[i+1]))
                             white++;
                        else if (color[i].equalsIgnoreCase(guess[i+1]))
                             white++;
                   if (black == 4)
                        anwser = "You WIN!!!!!!!! KIRBY DOES A SPECIAL DANCE FOR YOU \n (>'.')> (^'.'^) <('.'<) \n";
                        gamePlaying = false;
                        commandChecker = true;
                   else
                        turn++;
                        commandChecker = true;
                        anwser = "You got " + black + " black and " + white + " white it is your " + turn + " turn \n";
                   if (turn >= 10)
                        anwser = "You Lost =( , try again, the anwser was " + color[0] + color[1] + color[2] + color[3] + "\n";
                        gamePlaying = false;

    cotton.m wrote:
    if(guess.length!=4){
    // do something else besides evaluating the guesses. because something went wrong
    I should add that usually the best way of avoid array index out of bounds exceptions is to avoid ever hardcoding things like
    for(int i=0;i<4;i++)The 4 there is dangerous.
    It's safer to use the length
    for(int i=0;i<guess.length;i++)And the same applies to List(s) and the size method. Again usually that's a better idea but in your specific case anything more or less than 4 is not only a looping problem it would cause logical errors in the rest of your code... it is an exceptional case and should be dealt with specifically in your code.

Maybe you are looking for

  • My hp laserjet 6p prints test page, but not items in queue.

    My HP LaserJet 6P prints a test page when I push the button on the top, but does not print documents in the cueue.  I installed the latest drivers from HP to no avail.  I tried switching USB ports, also to no avail. I am able to delete documents from

  • Problem with invoice posting

    Hi .. i,m facing an issue with intercompany Invoice posting . I'm having delivery note as a reference to that invoice but im getting error message that delivery note doesnot exist..   i have entered correct delivery note..   how this can b overcome?

  • HCM in NetWeaver Trial Version

    Hi Experts, We are trying out with the Netweaver Trial version in our Team. ABAP and EP are working fine. As per the manual it is  mentioned that HCM is also included. But as such I see no signs of HCM in it and the T-codes does nt seem to exist. Is

  • Memory Leak with Oracle ODBC Driver for Long Raw columns

    Oracle version : 8.1.7 Platform : Win2K Oracle ODBC Driver version : 8.0.1.7.5.0.0 Hi, I've got an Oracle database upgraded from V8.0.5 to V8.1.7 which has a table having one long raw + normal columns. I was able to observe distinct memory leaks (app

  • Titles in digital photo album

    I'm trying to create a digital photo album on my iPad.  I've created an album in iPhoto, and then sync to my iPad and include that album in the sync.  The photos look great, but the titles have disappeared.  I need the titles to show.  Any ideas?