2

I pratcing regex, and I have a problem: I want to check if, if block is correct, meaning

if(a,){ 

would return false and

if(x, y){

would return true I came out with this regular expression :

(if|while)\s*(.*)\s*\{.*

but this return true on the first example, someone can help me to solve this problem?

Thanks George

3
  • what should be allowed between the braces, you only check .* Commented Jun 16, 2018 at 7:14
  • ups, too fast. (.*) is only a capturing group of anything, no braces are checked, they must be escaped with \ Commented Jun 16, 2018 at 7:38
  • You will soon see that regular expressions are not the right tool for parsing languages with a rich grammar. Commented Jun 16, 2018 at 7:46

2 Answers 2

1

The first problem with your regex is that your forgot to escape the brackets :).

Inside the brackets, you are just matching .*, which is everything except line endings, which doesn't seem to be what you want.

From your example, I would guess that you want to match those if statements where there is no trailing or leading commas just hanging there. So inside the brackets, this regex should match:

\w+\s*(?:\s*,\s*\w+)*

Explanation:

If we remove all the \s* (they are just there to allow whitespace everywhere), we get

\w+(?:,\w+)*

Basically, word characters, followed by a bunch of "comma and word characters" thingys. This ensures no comma at the end or the start because the end and start of the pattern are both \w.

We can just substitute that into your original regex:

(if|while)\s*\(\w+\s*(?:\s*,\s*\w+)*\)\s*\{.*

Demo

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

9 Comments

Thanks, It was almost what I meant,but I was able to modify what you gave to what I meant, thanks for the explaintation and the help
@Georg So what did you actually mean?
Maybe I didn't explained my self very well, but I check only the line of the 'if/while', and what you gave allowed if(x){ anything here, and I want it to be only if(x){
@Georg I see. So you added ^ and $ at the start and end?
no, just remove the '.*' at the end, but on a secound thinking, I still need to modify it, because I want to allow that there would at the end '//' and there anything after it. + there is one more problem, it doesn't allow ' if(a == b){ ', I would try to fix it, if I wouldn't be able to, I would write here again.
|
0

Welcome to Java's misnamed .matches() method ... It tries and matches ALL the input.

If you want to see if the regex matches an input text, use a Pattern, a Matcher and the .find() method of the matcher:

Pattern p = Pattern.compile("[a-z]");
Matcher m = p.matcher(inputstring);
if (m.find())
    // match

If what you want is indeed to see if an input only has lowercase letters, you can use .matches(), but you need to match one or more characters: append a + to your character class, as in [a-z]+. Or use ^[a-z]+$ and .find().

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.