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?
3 Answers
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.
2 Comments
Sidharth
actually the string is from user ..the user may or may not know regex pattern searching
Jon Skeet
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.
Use escapes?
1 Comment
Pascal Thivent
Who is the user? What are you actually trying to achieve? Please clarify your question and the problem you want to solve.
Pattern.compile()andMatherandString.indexOf().[\\\-\]\^(){}+*]