0

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?

9
  • You wanna search for occurrences of regular expressions in a given paragraph using regex? Bring down the constraints - what all can that regex contain? Commented Nov 11, 2009 at 12:34
  • A regex is a regex. There is no difference with a Java regex. Take a look at Pattern.compile() and Mather and String.indexOf(). Commented Nov 11, 2009 at 12:39
  • You can search for regex meta characters in the text to extract possible candidates. Use something like [\\\-\]\^(){}+*] Commented Nov 11, 2009 at 12:40
  • 2
    In your other post, stackoverflow.com/questions/1706775/…, I stressed the importance of clearly communicating your problem clearly to others through a public forum as SO. You either didn't read it, or ignored the advice. I don't really care but realize that many people who might be able to help you simply disregard question like yours because they ar too vague. Commented Nov 11, 2009 at 12:40
  • Bart ... i am really sorry ..this is Sidharth ..i am very new to this kind of sites (StackOverflow) .. now onwards i will communicate my problem clearly for sure .. sorry for inconveniences Bart ... Commented Nov 11, 2009 at 12:56

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

actually the string is from user ..the user may or may not know regex pattern searching
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.
2

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 =)

Comments

0

Use escapes?

1 Comment

Who is the user? What are you actually trying to achieve? Please clarify your question and the problem you want to solve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.