3

can anyone explain to me how to identify using regexes any string that contains for example a single P character in it? Could yo also explain how this is evaluated too?

EDIT: What is the difference between [^P*] and [^P]*?

1
  • 2
    please think of a better title of the question. Commented Jan 18, 2011 at 8:58

3 Answers 3

6

A string containing a single P character is:

  • a (possibly empty) string containing non-P characters
  • a P
  • a (possibly empty) string containing non-P characters

In a regular expression format, this is:

^[^P]*P[^P]*$

where:

  • ^ matches the start of the string
  • [^P]* matches any character other than a P, 0 or more times
  • P matches P
  • [^P]* matches any character other than a P, 0 or more times
  • $ matches the end of the string

The difference between [^P*] and [^P]* is:

  • [^P*] matches any single character that is neither P nor *
  • [^P]* matches zero or more characters that are not P

The placement of the * is of course important here. Inside [], the * doesn't have any special meaning. Outside [], the * means "match the previous thing zero or more times".

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

18 Comments

That doesn't seem to work on the regex checker..? Checking Here: gskinner.com/RegExr
Then there's something wrong with your regex chaecker, @david :-) I suggest you check it on the only checker that really matters, a Java program.
what is the difference between [^P*] and [^P]*
also what is the relevance of the initial ^ ?
@david robers - Everything is documented on the Pattern class - please, have at look at it first.
|
2

David, I think my answer at one of your previous questions will help you out. As for how it is evaluated, I'm not sure what you mean, but you might want to check out the API documentation for the Pattern class.

The difference between [^P*] and [^P]* is:

  • [^P*] means "one character that is not P and *"
  • [^P]* means "zero or more characters that are all not P"

2 Comments

Hi, I couldn;t get you answer to work on the regex checker either. I'm looking at the API but it doesn;t really help you understand how the regex engine works... I.e. does it start at the beginning of a string?
Generally yes, it does start at the beginning of the string, and each subsequent call to Matcher.find() will search out the next match. Please share the name of, or what sort of, regex checker you are using so we can help you more :-).
1

assuming you really want to check for a single "P". here's a non regex approach.

for(int i = 0 ;i< string.length(); i++){
    if ( string.charAt(i) == 'P' ){
        count++;
    }
}
if ( count == 1 ){
    System.out.println("String contains a single P");
}

3 Comments

string.contains("P") might be a slightly more simplified version :-)
well, I spoke too soon: yours makes sure that there is only one P. I take back my comment. Nevermind :-)
oh, i thought you are talking about using containS() in the for loop instead of charAt(), which i think is also valid.

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.