Hi everybody.

I am a student in computers at the Hebrew University in Jerusalem.

I started to work with Java - and working with Product Version:

NetBeans IDE 6.5
Java: 1.6.0_11; Java HotSpot(TM) Client VM 11.0-b16
System: Windows XP version 5.1 running on x86; Cp1252; en_US

I have an urgent question:

If I look for a patern in a scanner and I find it - How do I know the position in the scanner? the pattern helps me to be near data I know is afterward but i can not find a pattern or I dont know how it looks. so I look for the pattern (can be date) and I know that my data is afterwards and I want to collect from that position only for a number of bytes or until the end of a sentence..

I can't find any command - the start() and end() from the scanner are private. or can I access them?

Here is a function I wrote in Java:

private static String find(Scanner sc, String pattern) {
    String Stoken;
    while (( Stoken = sc.findInLine(pattern)) == null) {
        if (sc.hasNextLine()) {
            sc.nextLine();           
        } else {
            return null;
        }
    }  
    return Stoken;
}

Thanks

Dani AI

Generated

Short answer: as hinted, call Scanner.match() right after a successful find.../next... operation to get a MatchResult — that gives start(), end() and group() so you can locate the match and extract text after it.

Example using the scanner API (character offsets; works if you want the rest of the current line or a fixed number of characters after the match):

Scanner sc = new Scanner(new File("input.txt"), "UTF-8");
String found = sc.findWithinHorizon("(yourDateRegex)", 0); // or findInLine(...)
if (found != null) {
    MatchResult mr = sc.match();
    int matchEnd = mr.end();        // index after the matched text (character index)
    String restOfLine = sc.nextLine(); // returns the remainder of the current line
    String after = restOfLine.substring(0, Math.min(restOfLine.length(), desiredChars));
    String captured = mr.group(1);  // if you used a capturing group for the data you want
}

If you need precise, easy-to-reason-about offsets or more flexible extraction, read each line into a String and use Pattern/Matcher directly — then matcher.start()/end() are simple to use and you can substring() immediately.

Notes and cautions

  • MatchResult indexes are character offsets in the scanned text, not raw byte offsets. If you need byte positions (file offsets), read bytes (e.g., RandomAccessFile or FileChannel) and track positions yourself, or convert substrings with the proper Charset to get byte lengths.
  • Scanner.match() returns the result of the most recent scanning operation; call it immediately after the find/next that produced the match.
  • For complex extraction (until end of sentence, variable-length fields), prefer capturing groups in the regex (extract exactly what you need) or use Matcher on the whole line for clearer control.

Scanner.match() docs and regex guidance: see the Java Scanner and MatchResult docs and the Java regex tutorial for patterns and capturing groups.

You can get that info from the MatchResult returned by the match() method.

It would probably be a lot easier to add a capturing group to your match pattern for the data you want to collect and extract it as part of the match result. More info on capturing groups (and regex in general) here: http://java.sun.com/docs/books/tutorial/essential/regex/groups.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.