Home arrow static arrow Java Programming [Archive] - REGEX
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - REGEX
This topic has 6 replies on 1 page.

Posts:43
Registered: 10/4/03
REGEX  
Jul 31, 2004 10:15 PM



 
Hi guys. I'm just learning about regular expressions.
How can I do this...

An Example...
ImageIcon image1 = new ImageIcon("balloon.jpg");

replace with
ImageIcon image1 = new ImageIcon(getClass().getResource("balloon.jpg"));


If I could scan the file searching for
"new ImageIcon\("

then somehow preserve the string inside the quotation marks and replace with
"new ImageIcon\(getClass\(\).getResource\(STRING GOES HERE\)\)"


Thanks for your help.
Kyle--
 

Posts:11,200
Registered: 7/22/99
Re: REGEX  
Aug 1, 2004 12:16 AM (reply 1 of 6)



 
The replace methods allow back references to matching groups. Here's an example that puts parentheses around all strings of digits that are followed by either b or f:
String txt = "1ab2cd3ef456".replaceAll("[bf](\\d+)", "($1)");
 

Posts:43
Registered: 10/4/03
Re: REGEX  
Aug 1, 2004 11:56 AM (reply 2 of 6)



 
Could you please just show me how to do what I asked above?
Converting all occurrences of ImageIcon("some string here")
to ImageIcon(getClass().getResource("some string here"))

I don't really want to spend hours learning all the metachars and groupings, when one of you guys could just show me in 5 mins the code I need.

Thanks,
Kyle--
 

Posts:13,250
Registered: 24/10/97
Re: REGEX  
Aug 1, 2004 12:39 PM (reply 3 of 6)



 

I don't really want to spend hours learning all the
metachars and groupings, when one of you guys could
just show me in 5 mins the code I need.
So it is OK for us 'to spend hours and hours learning all the metachars and groupings' but not you! Come off it!

Looking at your OP I would have thought that all you need is a good text editor! If you don't have a good text editer and you want to do this in batch mode then why not 'find' and 'grep' or don't you want to learn about these either?
 

Posts:43
Registered: 10/4/03
Re: REGEX  
Aug 1, 2004 12:52 PM (reply 4 of 6)



 
I'm 18, just learning, get bent...
Sabre, obviously you don't have a solution so quit pretending like you are important.
You get off by making childish posts in threads were people really need help.

Jsalonen, thanks for your help.

Kyle--
 

Posts:447
Registered: 3/8/01
Re: REGEX  
Aug 1, 2004 1:01 PM (reply 5 of 6)



 
Groups are pretty simple. Anything you put within round brackets in a regex is assigned to a group, with the group numbers in order to the location of the opening parantheses. In your replacement, you can refer to groups using $n, where n is the group number. Therefore, first come up with a regex for your whole string:

new ImageIcon\("([^"]*)"\);


Then in your replacement put group 1 in the appropriate place:

new ImageIcon(getClass().getResource("$1"));
 

Posts:13,250
Registered: 24/10/97
Re: REGEX  
Aug 2, 2004 2:00 AM (reply 6 of 6)



 
I'm 18, just learning, get bent...
Sabre, obviously you don't have a solution so quit
pretending like you are important.
You get off by making childish posts in threads were
people really need help.
Ouch! Once again I am cut to the bone!

So being 18 is an excuse for being lazy!

The problem you are trying to solve can be tackled in many ways but all of them require either you to do the work or somebody else to the work. You have shown no attempt to solve the problem yourself and you will find few in this forum who are willing to give you a solution just because you are too lazy to do some reading yourself.

Post your attempt and people will rush to your door to try to help you.


 
This topic has 6 replies on 1 page.