Results 1 to 16 of 16
Thread: Java RegEx problem, need help
- 03-09-2012, 04:45 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Java RegEx problem, need help
Hi all,
i am trying to learning about regular expression in java. i have some problem, here my code :
and the result are :Java Code:import java.io.*; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.io.Console; class RegularExpression { public static void main(String args[]) { try { Console console = System.console(); Pattern pattern = Pattern.compile("ing|les"); Matcher matcher = pattern.matcher("About 2 months after being laid, the turtles hatchlings emerge from the sand and are put into sea water tanks"); boolean found = false; while (matcher.find()) { console.format("I found the text" + " \"%s\" starting at " + "index %d and ending at index %d.%n", matcher.group(), matcher.start(), matcher.end()); found = true; } if(!found){ console.format("No match found.%n"); } } catch (Exception e) { System.err.println(e); } } }
I found the text "ing" starting at index 23 and ending at index 26.
I found the text "les" starting at index 41 and ending at index 44.
I found the text "ing" starting at index 51 and ending at index 54.
my problem is, how to display the word "being" instead "I found the text "ing" starting at index 23 and ending at index 26." or "turtles" instead of "I found the text "les" starting at index 41 and ending at index 44.".
Excuse for my English.Last edited by DarrylBurke; 03-09-2012 at 09:45 AM. Reason: Added code tags
- 03-09-2012, 09:48 AM #2
Re: Java RegEx problem, need help
Look around the FAQs section of this site and discover how to use code tags. I've added them for you this time.
So, you want to capture all non-whitespace characters preceding "ing|or"? Or is it all non-punctuation|whitespace characters? or non-numeric characters?
First formulate your requirements. Then try to illustrate them with examples. Examples without specs are meaningless.
A couple of learning resources: Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns and Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-10-2012, 10:04 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Java RegEx problem, need help
hi db,
sorry for the mistake.
yes, i want to capture the characters preceding "ing|or"
-
Re: Java RegEx problem, need help
Have a look at this Regular Expressions Tutorial. I think that the shorthand character classes, word boundaries and the lookahead sections are likely most appropriate for your problem.
- 03-11-2012, 05:28 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Java RegEx problem, need help
why when i try to put "ing$" as the regex, it returns "No math found"
- 03-11-2012, 05:29 AM #6
Re: Java RegEx problem, need help
What did you think the regex "ing$" signifies?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-11-2012, 05:46 AM #7
Re: Java RegEx problem, need help
You call that a specification? Whitespace characters are characters. Alpha characters are characters. Digit/numeric characters are characters. Punctuation characters are characters.
Sowould mean from the beginning of input up to each "ing|or"the characters preceding "ing|or"
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-11-2012, 06:18 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Java RegEx problem, need help
regex "ing$" will return any words ended with "ing", am i right?
- 03-11-2012, 08:00 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 03-12-2012, 02:47 AM #10
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Java RegEx problem, need help
ooo i see,
then how to capture only the words ended with 'ing' ?
- 03-12-2012, 10:04 AM #11
Re: Java RegEx problem, need help
You've been given the links to two learning resources. One of the links has already been posted twoc.e
Are we to believe that you went through the material and are still unable to come up with an approximation of what might go into the regex? I'd sooner believe that you glanced at the pages and decided it might be easier to hang around here till someone spoonfeeds you the answer.
That's not going to happen. Not likely anyways.
And your specs are still vague and incomplete.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-13-2012, 04:28 PM #12
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Java RegEx problem, need help
yaay!! finally i solve it.
Thank you DB and JosAH.
it's not like i'm not read all those articles, i need sometime to understand it.
here is my code
Thanks for the help, i really appreciate it.Java Code:import java.io.*; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.*; class FileRead { public static void main(String args[]) { try { int count = 0; String txtFile; String str; Console console = System.console(); boolean found = false; while (true) { Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: ")); BufferedReader fileName = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Please enter directory of the file: "); System.out.flush(); txtFile = fileName.readLine(); FileInputStream fstream = new FileInputStream(txtFile); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while((str = br.readLine()) !=null){ StringTokenizer token = new StringTokenizer(str); while(token.hasMoreTokens()) { str = token.nextToken(); Matcher matcher = pattern.matcher(str); while (matcher.find()) { count++; System.out.println("i've found "+str); found = true; } } } System.out.println("Total word/s found= "+count); count = 0; if(!found) { System.out.println("No match found"); } } } catch (Exception e) { System.err.println(e); } } }
- 03-13-2012, 06:38 PM #13
- 03-13-2012, 06:50 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 03-13-2012, 08:20 PM #15
- 03-13-2012, 08:48 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
regex problem
By fishy8158 in forum New To JavaReplies: 14Last Post: 11-24-2011, 03:59 PM -
regex problem
By javaPower in forum Advanced JavaReplies: 7Last Post: 10-30-2011, 07:15 AM -
How to use regex and split to solve a problem
By PeteClimbs in forum New To JavaReplies: 2Last Post: 04-19-2011, 08:31 PM -
breaking up a string, a regex problem!!
By A.n.H in forum Advanced JavaReplies: 0Last Post: 05-17-2010, 03:03 PM -
Regex problem
By Nimyz in forum Advanced JavaReplies: 4Last Post: 05-14-2010, 07:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks