0

I was trying to write a regular expression for "[beginning word][specific word][ending word]" and making it case insensitive. But for some reason it doesn't seem to work.

I'm doing it in java, hence the extra "\".

For example, if I wanted to match the word "crazy" my regex would be:

/\\bcrazy\\b/i

My java code in which I call the regular expression where it doesn't match anything is:

textToMakeHarder = textToMakeHarder.replaceAll("/\\bcrazy\\b/i", "super crazy");
3
  • Not sure, but maybe end of line does not match end of word. Have you tried "super crazy me"? Commented Aug 31, 2012 at 17:57
  • Give us some sample text on which you wanna run this. Commented Aug 31, 2012 at 17:59
  • if textToMakeHarder = "Dragons dance crazy." it's supposed to replace "crazy" with "super crazy." I've tried it on dance and crazy with a couple different things to replace it with, but it's just not replacing them at all. replaceAll accepts (Regex patternToMatch, String toReplaceThePatternWhenMatched) Commented Aug 31, 2012 at 18:00

4 Answers 4

6

Try this:

  String textToMakeHarder = "I am a crazy person and he is also a crazy person. Are you crazycrazy too?";
  Pattern re = Pattern.compile("\\bcrazy\\b",Pattern.CASE_INSENSITIVE);
  Matcher m = re.matcher(textToMakeHarder);
  textToMakeHarder = m.replaceAll("super crazy");

Result:

I am a super crazy person and he is also a super crazy person. Are you crazycrazy too?

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

2 Comments

Ahhh, this is probably what I need! Let me try this and see if it works.
Yep. It's interesting that the /i stuff doesn't work. I got it fully working with this though. Thanks!
3

Get rid of the / and /i, and use a Pattern to get case insensitivity:

Pattern pattern = Pattern.compile("\\bcrazy\\b",Pattern.CASE_INSENSITIVE);
Matcher match = pattern.matcher(textToMakeHarder);

textToMakeHarder = match.replaceAll("super crazy");

Comments

2
Pattern pat = Pattern("\\bcrazy\\b",Pattern.CASE_INSENSITIVE);
Matcher mat = pat.matcher("Your_Input");

while(mat.find()){

   mat.replaceAll("super crazy");

}

4 Comments

I do need it to check that it has the beginning and ending of a word. For example, in the sentence "Are you sure you care?" I don't want the "are" part of "care" replaced by synonyms of "are." When I make "crazy" as a pattern can I still add the regular expression values for /\\bcrazy\\b/i around it to ensure it's not part of another word, and is case insensitive?
this will also match acrazya, which should not be matched
@CorayThan and Ankur Sorry dear, i just missed out the \\b , i am editing it now.
Keep in mind you need a look-ahead (?=\\b+) and a look-behind (?<=\\b+) for word boundaries. --- That code, as is, would find any matches to the pattern, including when they are not words themselves but are contained within the input as something else. --- For example: The phrase "Invalid input makes this program inoperable." has quite a potential for errors...A search for the word "valid" gives a match within "invalid"...If the search is for "in", hell breaks loose: "invalid input makes this program inoperable".
1

You'd want to try this simpler version:

String textToMakeHarder = "foo bar is CraZY tonight".replaceAll("(?i)\\bcrazy\\b", "super crazy");

Output is:

foo bar is super crazy tonight

Using the short modifier (?i) for case-insensitive search.

Reference to offficial Java 7 docs for Patterns

Comments

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.