-2

Im not receiving any answers from stackover. is anything went wrong with me..?

Please anyone help me in finding below Regex Problem.

If there is Input text starting with "(ix) (a) This is Sample Sentence." ,

it matches for both Regex Patterns

  1. ^9\.|^\s*[(](ix)[)]
  2. ^9\.\s*[(]?a[)]?|^\s*[(]\s*(ix)\s*[)]\s*[(]\s*(a)\s*[)]

Because my input sentence starting with (ix) and (ix) (a).

So please send me the regex which should match the input sentence.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Praveen Hiremath
  • 4,209
  • 2
  • 17
  • 11

1 Answers1

0

Matching method checks if entire String matches regex.

If you want regex to match all sentences that starts witch "(ix) (a)" like "(ix) (a) This is Sample Sentence." you can try something like:

String regex =
        "^" +                // start of new line
        "\\s*" +             // 0 or more spaces
        "\\(ix\\) \\(a\\)" + // "\\(" makes "(" normal character, not start of regex group
        ".*";                // 0 or more other characters (except for new line symbols)

String sentence= "(ix) (a) This is Sample Sentence.";

System.out.println(sentence.matches(regex));// output: true
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I will Explain in detail: Here is my code String regex1 ="^\\s*\\(ix\\).*"; String regex2 =^"\\s*\\(ix\\) \\(a\\).*"; List patternList = new ArrayList(); patternList.add(regex1); patternList.add(regex2); String sentence= "(ix) (a) This is Sample Sentence."; for(Iterator itr = patternList.iterator();itr.hasNext();) { String pattern = itr.next(); if (sentence.matches(pattern)) { System.out.println("*** Matched Pattern = " + pattern); } } "The result should match only regex2 not regex1". Here its matching both the regexes. Please help me out @"Pshemo" – Praveen Hiremath Sep 08 '12 at 10:44
  • Please update your post with that code because its very hard to read from comment. – Pshemo Sep 08 '12 at 10:52
  • @PraveenHiremath You can't make regex not to match `"^\\s*\\(ix\\).*"` if `"^\\s*\\(ix\\) \\(a\\).*"` matches. Could you tell more about your problem? Maybe you are trying to solve wrong thing? – Pshemo Sep 08 '12 at 11:03
  • I dont knw whether its rite or not, but i need regex which matches "^\\s*\\(ix\\) \\(a\\).*" but should not match "^\\s*\\(ix\\).*". – Praveen Hiremath Sep 08 '12 at 11:20
  • Please help me out to solve this. I think there is no problem exist without any solution .Please help me @Pshemo – Praveen Hiremath Sep 08 '12 at 11:24
  • Tell me why do you need such regex? What is its purpose? Maybe we will be able to think way around? – Pshemo Sep 08 '12 at 11:33
  • Could you gave me few examples of sentences that should and shouldn't be accepted by regex and explain why they are ok or bad? – Pshemo Sep 08 '12 at 11:43
  • Im doing Mapping. If input matches "^\\s*\(ix\) \(a\).*" im mapping to Element1 and If input matches "^\\s*\(ix\).*" im mapping to Element2. Based on Matched Element(Element1 or Element2) the further functionality works. – Praveen Hiremath Sep 08 '12 at 11:44
  • So first try to check if your sentence matches regex2 (because it is more detailed). If not then check if it matches regex1 (it is more general) – Pshemo Sep 08 '12 at 12:05
  • Thanks very much Pshemo for ur replies. I Could do that. But i will not be knowing the sequence of regexes or Elements. More over these regexes are exposed to the user. So he can enter it any where rite. There is no solution for my problem? – Praveen Hiremath Sep 08 '12 at 12:29
  • Probably there is solution for your problem but it wont be so simple as I thought. Maybe other SO users will be able to help you, so try to create another question but this time put more details in it because right now it is very hard to understand what is your goal. – Pshemo Sep 08 '12 at 12:40