Results 1 to 4 of 4
- 06-03-2011, 01:55 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
How to find string pattern written between 2 ### in a paragraph
Hi,
The project i'm working on is like this..
i have a String variable which stores something like a paragraph. from this i need to find all the occurances of the substring ###_______###. the value between ### can be anything..
i was thinking of using the split() function and comparing if each string stored has 2 ### patterns. but this seems a very inefficient way of coding, especially since the number of words can be huge.
can u please suggest any approach or function that i could use.. i don't know much abt regex, i have tried searching, but haven't really found anything useful yet..
thanks in advance..
- 06-03-2011, 02:22 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
I'm not sure what you want but finding the following three substrings is quite easy:
which returns the output:Java Code:import java.util.ArrayList; public class Paragraph { public static void main(String[] args) { ArrayList<String> strings = new ArrayList<String>(); String s1 = "###substring###"; String s2 = "###example###"; String s3 = "###whatever###"; strings.add(s1); strings.add(s2); strings.add(s3); for (String s : strings) { int indexOfFirstChar = s.indexOf("###")+3; int indexOfLastChar = s.indexOf("###", indexOfFirstChar)-1; System.out.println(s.substring(indexOfFirstChar, indexOfLastChar+1)); } } }
substring
example
whatever
- 06-03-2011, 02:49 PM #3
Start learning here:i don't know much abt regex
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
and of course the Pattern API.
What you want is a regex that reluctantly matches anything, with positive look-behind and look-ahead that match '###', and to use that with Matcher#find() in a loop.
db
- 06-03-2011, 02:57 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
thank you for the suggestion and the link, the index method might help. Actually the paragraph i mentioned is an xml converted to a string. i'll have something like <Name>###Black###</Name>. Once i find the pattern i need to check if the opening and closing tags are the same.. any suggestion on how i could try that would be a great help..
Similar Threads
-
using a loop to form a string pattern
By xquizitpinay in forum New To JavaReplies: 7Last Post: 10-29-2010, 04:06 AM -
find index of string in another string
By Sdannenberg3 in forum New To JavaReplies: 4Last Post: 03-04-2010, 10:14 AM -
String matching a pattern
By vividcooper in forum New To JavaReplies: 7Last Post: 01-07-2010, 10:30 PM -
java regular expression to search block/string/word exist in a paragraph?
By sidharth in forum Advanced JavaReplies: 2Last Post: 11-11-2009, 05:56 AM -
confusion in paragraph
By JavaJunkie in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-19-2009, 03:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks