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 3 of 3

Thread: regular expressions

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default regular expressions

    ok straight to the problem, i have this String that my program reads from a file:

    package javaapplication1;
     
    public class Main {
     
        public static void main(String[] args) {
          String example = "0001Brad Lambert        00000200.0008081987blambert@my.wctc.edu";
          int tmpa,tmpb;
          String tmp;
          String nameexp = "[A-Z][a-zA-Z]{3,} [A-Z][a-zA-Z]{3,}";
     
               tmpa = example.indexOf(nameexp);
                tmpb = example.lastIndexOf(nameexp);
                System.out.println(tmpa);
                System.out.println(tmpb);
                tmp = example.substring(tmpa, tmpb);
                System.out.println(tmp);
        }
     
    }
    im trying to use a regular expression
    to pulll out the name "Brad Lambert" to a temporary string value
    problem is tmpa and tmpb keep returning a value of -1.
    Last edited by brad35309; April 5th, 2010 at 12:08 PM.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: regular expressions

    To get better help sooner, post a SSCCE* that clearly demonstrates the problem and what you've done so far. Without that, members here would be simply guessing.
    * (link) SSCCE : Java Glossary

    db

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: regular expressions

    indexOf does not use regular expressions. See Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes) for information on how to use regular expressions in java. Alternatively you could go the messy way and split the string, get the flanking strings and their indexes to rebuild the string found with the regex.

Similar Threads

  1. Text Processing with Regular Expressions explained in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: February 8th, 2022, 05:16 PM
  2. Replies: 3
    Last Post: December 22nd, 2011, 09:46 AM
  3. [SOLVED] Java Regular Expressions (regex) Greif
    By username9000 in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 05:53 PM