• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write regular expression to skip a specific charaters in word

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Check this. hope some one really simplifies it.


Welcome to the Ranch
 
Jigar Damani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you can dynamically construct the regular expression.

I wonder what you need this for though.
 
Jigar Damani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. it wont work for other patterns. But i dont know if there is a straight replacement to exclude special chars.

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can copy the string, delete all special characters you want to ignore in the string, and then apply the pattern.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, take a look at this snippet and let us know if you understand what is going on:
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan,
i am new to this Scanner technology. thanks for sharing. I dont understand this one. I have to learn it.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the documentation on java.util.Scanner. It's a wonderful tool.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic