简体   繁体   中英

Regular expression searching a string?

I need to be able to create a regular expression that will search through any string in Java and match this particular expression.

Reg Match: ./ ADD NAME= <--- I need to match this regular expression in any string that I read into the file and it normally would be in position 1 through 13

I am looking for this in the first 10 characters of any string because I have to return the name that follows the equals sign to make a file name with so the whole line would looking like the following.

Question: Why does my expression not match in any of the strings?

File Line:

   ./ ADD NAME=FILE02 
        ...more records would go here...
   ./ ADD NAME=FILE332 
        ...more records would go here...
   ./ ADD NAME=FILE3 
        ...more records would go here...
   ./ ADD NAME=FILE-1A
        ...more records would go here...

Code:

    private void locatedFileName(String s) {
    Pattern p = Pattern.compile("./ ADD name=");
    Matcher m = p.matcher(s);
    boolean b = m.matches();

    if (b) {
        System.out.println("True");
    } else {
        System.out.println("False");
    }
}

**private void locatedFileName(String s) {
        if (s.matches("./ ADD name=")) {
            System.out.println("True");
        } else {
            System.out.println("False");
        }
}**

All the Code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class test {

    test(){
    }

    private void locatedFileName(String s) {
        if (s.matches("./ ADD name=")) {
            System.out.println("True");
        } else {
            System.out.println("False");
        }
    }

    private void testReadTextFile() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\itpr13266\\Desktop\\TestFile.txt"));
            String line = null;
            //Will read through the file until EOF
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                locatedFileName(line); //test to see if the "./ Add name=" is found in any of the strings
            }
        } catch (IOException e) {
            System.out.println("Try-Catch Message - " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main(String[] a) {
        test t = new test();
        t.testReadTextFile();
    }
}

Newest Code that I tried:

code:

        final String REGEX = ("(?i)\\./\\s+ADD\\s+NAME\\s*=");
    final String REGEXtemp = ("\\s+ADD\\s");

    //Pattern p = Pattern.compile(REGEX);
    Pattern p = Pattern.compile(REGEXtemp);
                Matcher m = p.matcher(s);
    boolean b = m.matches();

    if (b) {
        System.out.println("True");
    } else {
        System.out.println("Reg not found in string!");
    }

info -
Try it like this to get the substring:

 Pattern patt = Pattern.compile("(?i)\\./\\s+ADD\\s+name\\s*=");
 Matcher m = patt.matcher( s );
 while (m.find()) 
 // or if (m.find())
 { 

 }

Matches I think has to match the whole string (gotcha)
to match the whole thing. For that use this regex:

 Pattern.compile("(?si).*?\\./\\s+ADD\\s+name\\s*=.*");

Your regex should have matched, even with the dot metachar you've used.
It's possible there are tabs instead of spaces there.

I guess you could replace space literal with whitespace class.
Something like this:
edit - Oh, you need case insensitive too
because ADD,name doesn't match ADD,NAME

 #  Pattern.compile("(?i)\\./\\s+ADD\\s+name\\s*=");

 (?i) \./ \s+ ADD \s+ name \s* =

Expanded regex with comments

 #  Pattern.compile("(?i)\\./\\s+ADD\\s+name\\s*=");

 (?i)      # Case insensitive modifier
           #     for all subsequent alpha literals
 \.        # literal dot
 /         # literal forward slash
 \s+       # 1 or more whitespace characters
 ADD       # 'ADD'
 \s+       # 1 or more whitespace characters 
 name      # 'name'
 \s*       # 0 or more whitespace characters
 =         # '='

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM