aspose file tools*
The moose likes Beginning Java and the fly likes Regular Expression Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regular Expression Query" Watch "Regular Expression Query" New topic
Author

Regular Expression Query

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi,

I need to search for string in the form of
1. name="<anything>"
2. name="<anything.anotherThing>"

I am using the following code--




It's giving output like--




Can you please point out where i m doing mistake?

Thanks,
Kaya.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

How about this RE:



I suppose '?' stands for 0 or 1 times


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Let's investigate your regex.

(name): the literal text name, grouped for some reason. So far so good.
=: the literal = symbol. So far so good.
- ([[\"]\\w.w[\"]]+): a group you want to use to capture the value. Let's dissect that:

[[\"]\\w.w[\"]]+: you have two character classes ([\"]) inside another character class. This will create a union of the character classes. In other words, this regex is saying: one or more characters from the character class that contains a double quote, a word character, a dot, a literal w and (again) a double quote. In other words, it doesn't stop at a double quote if another dot word character follows.


Let's re-create your regex.

- part 1: the literal "name": name
- part 2: the literal "=": =
- part 3: your group:
-- part 3.1: start with a quote: "
-- part 3.2: anything: \w+
-- part 3.3: a dot followed by anotherThing: \.\w+. However, it is optional, so it becomes (\.\w+)?
-- part 3.4: end in a quote: "

Put that together, and you get name=("\w+(\.\w+)?")
Put that into a Java string (so escape the " and \ characters with another \) and you're done. Note however that you'll now need to use group(1); group(2) is now the optional .anotherThing part. You can help your regex engine a bit by telling it to ignore the last group by putting a ?: right after the parenthesis: name=("\w+(?:\.\w+)?")
This way, group(0) will be the entire matched string, group(1) will be the thing you want and group(2) and up do not even exist.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Thanks a lot Rob for the descriptive solution.
It was really very helping content you wrote.
Can you please share the link to read more about regular expressions.

Thank you Sagar for your quick response.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

java.util.regex.Pattern has a lot of information on regular expressions in Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular Expression Query