-1

Here is my code

 Pattern pbold = Pattern.compile(".*\\* *(.*?) *\\*.*");
 Matcher mbold = pbold.matcher(s);
 mbold.find();
Ayush
  • 173
  • 1
  • 1
  • 12

3 Answers3

1

What you need is the metacharacter that matches whitespaces charaters: (?s) This whitespace metacharacter matches:

  • A space character
  • A tab character
  • A carriage return character
  • A new line character
  • A vertical tab character

For more info about this special characters, please consult The Java Tutorials - Regular Expressions - Predefined Character Classes.

The code belows matches the case you need:

String s = "abc021\n" +
            "34-+\n" +
            "*\n" +
            "a\n" +
            "p\n" +
            "p\n" +
            "l\n" +
            "e\n" +
            "*\n" +
            "fga32\n" +
            "49";
    Pattern pbold = Pattern.compile(".*\\* *((?s).*?) *\\*.*");
    Matcher mbold = pbold.matcher(s);
    mbold.find();

There is also a similar question here: Regular expression does not match newline obtained from Formatter object

Community
  • 1
  • 1
Esteban
  • 74
  • 4
  • I want `*apple*` will be became *apple* in bold without stars . – Ayush May 23 '16 at 10:05
  • Thanks for your feedback @AyushSaxena. The regex above matches words between stars even across lines. I need precise description on your **input** and **expected output** to give you better feedback. – Esteban May 23 '16 at 10:27
  • no consequtive ** will be replace I have done this Pattern pbold = Pattern.compile(".*\\* *(.*?) *\\*.*"); – Ayush May 23 '16 at 10:43
  • but this replace ** also to bold ** = nothing hope you understand – Ayush May 23 '16 at 10:44
  • If I understood correctly: INPUT: `I want *apple* and **orange** in bold` OUTPUT: I want **apple** and **orange** in bold The problem is **Java regex does NOT support recursive patterns**; i.e. it's not possible to recognize something like `**apple**` with a variable number of stars (e.g. `***apple***`). My suggestion to simplify this regex is to match the innermost pair of stars. For example: INPUT: `I want *apple* and **orange** in bold` OUTPUT: I want **apple** and * **orange** * in bold – Esteban May 23 '16 at 11:58
  • Another possibility is to extend the code that matches words/phrases to be set in bold, to match multiple stars. But unfortunately you have to do it manually. An also consider the following case: INPUT: `*apple* and **orange** match *fruit**` OUTPUT: **apple** and **orange** match **fruit** * – Esteban May 23 '16 at 12:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112681/discussion-between-esteban-and-ayush-saxena). – Esteban May 23 '16 at 12:07
0

Use flags igm like below:

Pattern pbold = Pattern.compile(".*\\* *(.*?) *\\*.*");
 Matcher mbold = pbold.matcher(s, Pattern.MULTILINE|Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
 mbold.find();
nikli
  • 2,281
  • 2
  • 24
  • 38
  • 1
    `Pattern.MULTILINE|Pattern.CASE_INSENSITIVE` are completely redundant here. None of the tokens used in the regex is affected by these 2 flags. – nhahtdh May 23 '16 at 09:09
0

This regular expression might solve your problem...

 Pattern pbold = Pattern.compile(".*\\*[ \n]*(.*?)[ \n]*\\*.*");
 Matcher mbold = pbold.matcher(s);
 mbold.find();

If this doesn't solve it..please elaborate what you are trying to get through this expression.

Rishabh Gupta
  • 136
  • 1
  • 11