0

I have a Mongo DB column which contains values of the sort "1001,100" and "1001" stored as a string. While searching for '100' through a java class, I have used

query.put(dbColumn, java.util.regex.Pattern.compile(object));

where object is 100. This query results in two values as it searches for 100 and 1001 contains 100. Is there any way just get the value that contains only 100 as part of its string using regex (i.e. the result should only be "1001,100")?

2
  • I am not sure I understood you correctly. I assume you want to match 100 and only 100 in a string that contains 1001 001100 and 100 right? Commented Nov 11, 2011 at 14:34
  • Are you trying to match the entire string and not just a subset? It would be generally helpful if you could provide some more examples of expected inputs and outputs. Commented Nov 11, 2011 at 15:09

1 Answer 1

1

I understand you to be saying that your Mongo DB column can contain multiple values which are comma-separated. For example, the string "1001,100" represents a set of values, 1001 and 100. You want to search for a particular value, such as 100, in the column, but only a complete value, not a partial value, should be counted as a match.

If I have understood correctly, then in your regular expression you need to declare that the value you are searching for is a complete value, not a partial value. You do this by adding a word boundary at the start and end of the value:

\b100\b

This will cause the search to match only complete values.

The reason this works is that the \b operator matches a word boundary. A word boundary is a place in the input where there is a word element on one side of the boundary and a non-word element on the other side. So \b100\b will only match when the 100 has non-word elements on both sides – in your case, commas or the beginning or end of the field.

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

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.