9

Please can someone tell me how to match "_" and a period "." excatly one time in a string using regex, Also is it more efficient using indexOf() instead of regex expression.

String s= "Hello_Wor.ld"  or 
s="12323_!£££$.asdfasd"

bascially any no of characters can come before and after _ and . the only requirement is that the entire string should only contain one occurance of _ and .

1
  • you should be more concerned about which one is easier to read and comprehend the intention of and thus easier to maintain, in most logic cases regex will lose this consideration in straight matching cases regex will probably win. Commented Nov 16, 2011 at 19:42

2 Answers 2

10

indexOf will be much quicker than a regex, and will probably also be easier to understand.

Just test if indexOf('_') >= 0, and then if indexOf('_', indexOfFirstUnderScore) < 0. Do the same for the period.

private boolean containsOneAndOnlyOne(String s, char c) {
    int firstIndex = s.indexOf(c);
    if (firstIndex < 0) {
        return false;
    }
    int secondIndex = s.indexOf(c, firstIndex + 1);
    return secondIndex < 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I can now safely remove my answer! :)
2

Matches a string with a single .:

/^[^.]*\.[^.]*$/

Same for _:

/^[^_]*_[^_]*/

The combined regex should be something like:

/^([^._]*\.[^._]*_[^._]*)|([^._]*_[^._]*\.[^._]*)$/

It should by now be obvious that indexOf is the better solution, being simpler (performance is irrelevant until it has been shown to be the bottleneck).

If interested, note how the combined regex has two terms, for "string with a single . before a single _" and vice versa. It would have six for three characters, and n! for n. It would be simpler to run both regexes and AND the result than to use the combined regex.

One must always look for a simpler solution while using regexes.

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.