rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Apr 16
2009

Java Regex Matching digg

Took me half a day to figure out matcher.find() had to be called first. Gaah!

RegexTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
public class Tester {
    public static void main(String args[]) 
    {
        Pattern pattern = Pattern.compile("name=\"QTime\">(\\d+)</int>");
        Matcher matcher = pattern.matcher("<response><lst name=\"responseHeader\">" + 
            "<int name=\"status\">0</int><int name=\"QTime\">2</int></lst></response>");
 
        try {
            if(matcher.find()) {
                System.out.println(matcher.group(1));
            }
        }
        catch (Exception e) {
            System.out.println("Couldn't find QTime");
        }
    }
}

P.S. I love Python

5 Responses (rss) (trackback)

#1

Shashi

April 18th, 2009 at 7:45 am

Why? Google.com is banned out there? ;-)

#2

Pravin Paratey

April 25th, 2009 at 12:14 am

:P

#3

Santosh

May 8th, 2009 at 2:24 am

Hey, recalling our mysites days!! regex and pattern matching. :) )

#4

Pravin Paratey

May 8th, 2009 at 2:51 am

Haha :P Those were some lovely days :)

#5

Lee

April 9th, 2010 at 9:02 pm

Haha, that happened to me the first time that I tried working with Java’s pattern object. I was used to the python paradigm of working with regex and couldn’t for the life of me figure out what went wrong xD Good times, good times.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Apr
07

Palindromic sub-sequences in python

This bit of python code returns all palindromic subsequences in the input string.

[Read More]
Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it!

[Read More]

Featured Projects

Indic to English Transliterator

Indic to English Transliterator

Transliteration is the process of converting a word from one language to another while retaining its phonetic characteristics. This application lets you convert a word from any major Indian language (currently supports Hindi, Marathi, Sanskrit and Bengali) to English.

[Read More]

NLP classes for PHP

NLP classes for PHP

This is an ongoing project to develop a set of classes for Natural Language Processing. Some code would be ported from the NLTK project.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.