tags:

views:

83

answers:

3

Hi i need to search a string with in paragraph .Here the string may be a java regex . How do i differentiate a regex and a ordinary string. Please give some suggestions to achieve this?

+4  A: 

A regex is an ordinary string. For example, if you get given "a.b" should that match "axb" or not? It does if you treat it as a regular expression, it doesn't otherwise. If you want to actually search for just the value "a.b" then what would you want to be passed?

Where is the string coming from? Ideally, get that data source (whether it's the user or not) to tell you whether to use a regex or not.

Jon Skeet
actually the string is from user ..the user may or may not know regex pattern searching
Sidharth
Well only the user knows whether they expect "a.b" to match "axb" then. The user *has* to give you that information - e.g. by having a checkbox saying "Treat this as a regular expression", defaulting to false.
Jon Skeet
yes this is a good solution
Sidharth
A: 

Use escapes?

Pascal Thivent
But the user may not know about the escapes..
Sidharth
Who is the user? What are you actually trying to achieve? Please clarify your question and the problem you want to solve.
Pascal Thivent
yes u r right .. this works good ..
Sidharth
+2  A: 

Seems impossible. In fact words like I, am, a and developer are also perfectly valid regexes.

You need to tell first what patterns you'd expect to be regex patterns, then we can maybe suggest another regex pattern for this =)

BalusC