| Author |
How to write regular expression to skip a specific charaters in word
|
Jigar Damani
Greenhorn
Joined: Jul 04, 2011
Posts: 8
|
|
Hi,
I would like to writer regular expression to find word by neglecting specific characters in word.
Example:
I want to search word 123456 in a paragraph. But in paragraph, I have workds 123-456, 1234-56 etc. But I would like to search these words also by skipping hyphen (-).
How to do this?
Thanks in Advance
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
|
Hi,
Check this. hope some one really simplifies it.
Welcome to the Ranch
|
 |
Jigar Damani
Greenhorn
Joined: Jul 04, 2011
Posts: 8
|
|
Thanks for your reply..
but this pattern will work for only 123456. I have given example of 123456 but I want to make it dynamic.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
Then you can dynamically construct the regular expression.
I wonder what you need this for though.
|
 |
Jigar Damani
Greenhorn
Joined: Jul 04, 2011
Posts: 8
|
|
Ok...I have to construct regular expression dynamically...
To construct expression dynamically, I would need so many string operation.
so is there any thing that can work without creating regular expression dynamically?
any work around?
Thanks
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
|
Agreed. it wont work for other patterns. But i dont know if there is a straight replacement to exclude special chars.
|
 |
Daniel Marti
Ranch Hand
Joined: Jun 08, 2011
Posts: 37
|
|
|
You can copy the string, delete all special characters you want to ignore in the string, and then apply the pattern.
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
|
Daniel Marti wrote:You can copy the string, delete all special characters you want to ignore in the string, and then apply the pattern.
hi, how to know which string to copy. It can lead to copying all words in the file to a string & then removing the special characters if we need a file search. Correct me
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
|
John, take a look at this snippet and let us know if you understand what is going on:
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
|
Stephan,
i am new to this Scanner technology. thanks for sharing. I dont understand this one. I have to learn it.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
|
Read the documentation on java.util.Scanner. It's a wonderful tool.
|
 |
varun narisetty
Greenhorn
Joined: Oct 14, 2009
Posts: 8
|
|
HI friends,
I am also having similar problem, I need to get the substring from a string which matches a particular pattern.
consider an example of string like this String htmlContent = "<p> This is first paragraph.</p> <p> This is another new paragraph</p>" ;
Now i will search for String word =" first paragraph. This is another new";
now i need to get result as "first paragraph.</p> <p> This is another new"
I have tried scanner like this
Scanner scanner = new Scanner(htmlContent);
System.out.println("Content is :"+scanner.findWithinHorizon(word, 0));
I am getting null.
I will be thankful if someone help me how to proceed for this problem.
thank in advance.
|
 |
 |
|
|
subject: How to write regular expression to skip a specific charaters in word
|
|
|