0

I'm having trouble grabbing a specific piece of text.

My input is:

PMT(B1+B144+B145*1/12.0,B148+B149*1*12.0,B1)

I want to grab all the B1's, but when I'm trying to do that I'm getting B1, B144, B148, B1. My first solution was to check the following character. So I came up with the regex B1[\W]. There are two problems with this: One it ends up grabbing the non word character, and two it doesn't work with "=B1".

How can I grab specific B1s? For this example I want the first and last B1.

Edit: I'm using the Java String function replaceAll

2
  • 1
    My eyes hurt. Anyways, what language/flavor are you using ? Commented Jul 15, 2013 at 13:22
  • I'm using java String function replaceAll Commented Jul 15, 2013 at 13:25

2 Answers 2

2

Use B1(?!\\d) which means:

  • B1 : match B1
  • (?!\\d) : not followed by a digit
Sign up to request clarification or add additional context in comments.

Comments

2

Make sure you use word boundaries:

String repl = str.replaceAll("\\bB1\\b", "");

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.