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: Regular expression to find a line beginning with a certain character Using Java

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Angry Regular expression to find a line beginning with a certain character Using Java

    Hello guys,

    I have a text file that has the following lines:

    the boy is smart
    He is from Australia
    ** Highly important line
    That's all

    Now, I need a regular java expression that would match a line in this file that begins with the ** characters. That is, when I match the text with the regex, I should get only this line :

    ** Highly important line

    How would I write this regex in java?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Regular expression to find a line beginning with a certain character Using Java

    You have to use a regular expression? It'd be much simpler (IMO) to just use an if statement that looks for "**" at the beginning of the line.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    help_desk (July 12th, 2014)

  4. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Regular expression to find a line beginning with a certain character Using Java

    How do you check that the line begins with "**" in java? Any simple example?

    Thanks

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Regular expression to find a line beginning with a certain character Using Java

    Look at the String API first. Read what each of the methods do, thinking about what you're trying to accomplish, and sketch out something you think will do that - just write pseudo-code if that's all you want to attempt. Come back with your rough outline if/when you need more help.

  6. #5
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Regular expression to find a line beginning with a certain character Using Java

    Hi,
    I do know there is the contains() method that is very close to what I need but that will not specifically check for the first characters in a line. I don't know of any String API that could do that except to split the line and check the [0] of the String array and see if it matches **" but I wanted to see IF I could just compare the line with some regex that begins with "**"

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Regular expression to find a line beginning with a certain character Using Java

    Okay, try that.

  8. #7
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Regular expression to find a line beginning with a certain character Using Java

    Like I said, I do know how to split each word in the line and check the first index of the String array.I was just looking for a way to compare each line to a particular regex line that starts with "**". Then you said there is a String API method that could do this? So, I wanted to know if truly there is a string method that can just easily check the first word in a line but found nothing.

    --- Update ---

    Hello GregBrannon,

    Could I ask you for a twist to the problem I am still solving. This is a continuation to the thread but I have come to a stage in the problem where I need some kind of help or pointer to where I could get help. I have an array i-6.. Now, these i is a representation of the alphabets A-F. Now, how do I convert this i to a character. I do know to convert from a character to an integer one does something like this: char-'a' to get the integer representation. How does one convert the integer back to a character?

    Thanks

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,974
    Thanks
    52
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Regular expression to find a line beginning with a certain character Using Java

    after changing the value of the int, cast it to a char: (char)50 is '2'
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    help_desk (July 12th, 2014)

  11. #9
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Regular expression to find a line beginning with a certain character Using Java

    Hi Norm,

    Thanks alot. I figured out how to do that with the below code :
    char c = (char) ('A'+ i);

Similar Threads

  1. Java String Regular Expression Help
    By Raj4Smile in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 12th, 2014, 08:04 AM
  2. Regular Expression in Java for HTML pages
    By pari89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2014, 07:15 AM
  3. validate a String in java Regular Expression
    By khalidhabib in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2013, 04:45 AM
  4. Replies: 3
    Last Post: September 30th, 2011, 08:45 AM
  5. Using Regular Expression (regex) in Java Programming
    By lordelf007 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 14th, 2010, 10:29 AM

Tags for this Thread