0

I am trying to write a regex expression to replace the following in a space delimited file

The file has:

WORD WORD C F F ANOTHER WORD

ANOTHER WORD D H F ANOTHER ANOTHER

I am trying to get the expression to capture the LAST F between F F and H F.

I wrote this expression but it doesn't process correctly in JAVA.

\\(\\(?:[a-z][a-z0-9_]*\\)\\)(\\s+)(F)(\\s+)

Can anyone help out? I'm trying to use this with String.ReplaceALL.

Thanks.

8
  • 2
    I see no F between F F and H F. Commented Sep 16, 2011 at 14:19
  • There are no F's between "F F" and "H F" in your test file... At any rate, it's be something like .*H F.*(F).*F F.* dot-star matches anything... Commented Sep 16, 2011 at 14:19
  • Why are you escaping your capture parens? Doesn't seem like you are looking for parens in your string. Commented Sep 16, 2011 at 14:20
  • This is pointless anyway. Let's say there were two "F"s in the string. What would you get from being able to capture the last one? It is returned as a string so you get "F". What does that tell you? Do you want the number of "F"s in your capture? The position of the last "F"? Commented Sep 16, 2011 at 14:31
  • Gents...my bad writing. I want the last F and replace with Female or if M I want Male. Commented Sep 16, 2011 at 14:35

3 Answers 3

1

((?:[a-z][a-z0-9_]+))(\\s+)(F)(\\s+)

This is the translation of what you have that is a valid regex.

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

Comments

0

How about this one :

\\b(F)\\b(?!\\s+\\bF\\b)

looking for standalone F not followed by another standalone F

1 Comment

my pleasure, how about a tick on my answer ;-)
0

This one matches such strings. But how do you replace?

"^.* F ([^ ]*|[ F])*$"

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.