Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to define a string pattern using variables?

  1. #1
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to define a string pattern using variables?

    Hi guys

    Firstly Happy new year to you soon, can someone please tell me this:

    I understand how to write a pattern with single character of regular-expression (eg. [a-zA-Z]), but how to define the pattern with my own varibles?

    Eg. I got 3 variables
    String roadName, city, cityDistance;
    , how to define below string using my own variables?

    <roadName> <cityA> <AB distance> <cityB> [<BC distance> <cityC> […]]

    Many Thanks
    Last edited by ice; December 30th, 2010 at 11:10 AM.


  2. #2
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: How to define a string pattern using variables?

    i don't understand what you want. Can you describe clearly again?

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to define a string pattern using variables?

    You can concatenate Strings whether they are stored as String variables or as literals. Try using the + operator or using a StringBuilder.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to define a string pattern using variables?

    Thanks KevinWorkman, I didn't express my question well, I mean how to write a general pattern for a given string with string variables. For example:
    Here are some strings, each string represent one road name:

    "OR Adelaide 300 Melbourne"
    "HH Melbourne 850 WestSydney 105 Sydney"
    "BushTrack Adelaide 2448 Brisbane"
    ....

    Obviously they have similar form, how do I write just one pattern in java that can represent any of above road?

    Thanks heaps

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to define a string pattern using variables?

    When coming up with a regular expression, it oftentimes helps to write out the rules in English first. What exactly is the form? Is it "any String followed by a number followed by any String", or does white space count, or are numbers even required, or is there anything else you want to take into consideration? What kinds of things do you NOT want the pattern to accept?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to define a string pattern using variables?

    ok, the english rule for a raod descriorion is:
    <roadName> <cityA> <AB distance> <cityB> [<BC distance> <cityC> […]]

    But how to write just one pattern in java that can represent above?

    Thanks heaps

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to define a string pattern using variables?

    What would be an acceptable file you would need to parse (or ones your program should be able to recognize errors in and handle them gracefully)? Sometimes regular expression are over-kill and make the problem too hard.

    For example, if the file contains all the cities connected to a particular road, and then the distance between cities by taking that road all on a single line (or separated by some special character) then you could simply hard-code a Scanner object to read in items, determine what your current state is (are you looking at/for a road, a city, or a distance?), then mark that object as such.

  8. #8
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: How to define a string pattern using variables?

    Quote Originally Posted by ice View Post
    Thanks KevinWorkman, I didn't express my question well, I mean how to write a general pattern for a given string with string variables. For example:
    Here are some strings, each string represent one road name:

    "OR Adelaide 300 Melbourne"
    "HH Melbourne 850 WestSydney 105 Sydney"
    "BushTrack Adelaide 2448 Brisbane"
    ....

    Obviously they have similar form, how do I write just one pattern in java that can represent any of above road?

    Thanks heaps
    Sometimes regex is not the right tool for the job. Where does you strings come from ? a text file?
    Last edited by JavaHater; January 7th, 2011 at 09:25 PM.

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: How to define a string pattern using variables?

    Quote Originally Posted by KevinWorkman View Post
    You can concatenate Strings whether they are stored as String variables or as literals. Try using the + operator or using a StringBuilder.
    You can also use the String method concat(String str).

    Just be sure that when you concatenate them, you do something like

    str = str.concat(str2);

    or

    str = str + "a Message";

Similar Threads

  1. Help with regex pattern
    By b_jones10634 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 24th, 2010, 03:59 PM
  2. a new pattern with a new architecture
    By mcgrow in forum Web Frameworks
    Replies: 0
    Last Post: August 4th, 2010, 02:28 PM
  3. [SOLVED] Need Regex for a pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 7
    Last Post: May 5th, 2010, 02:06 AM
  4. Can Define what is this error
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 18th, 2009, 01:01 AM
  5. ERROR, I CANT DEFINE IT
    By chronoz13 in forum AWT / Java Swing
    Replies: 7
    Last Post: December 2nd, 2009, 09:47 AM