How to make a regex that accepts one line but not two?

Asked

Viewed 84 times

1

I get lost doing any complex regex.

I use the following code in java:

Pattern pattern = Pattern.compile("tentativas de regex aki");
Matcher matcher = pattern.matcher(conteudo);
while(matcher.find()) {
   System.out.println(matcher.group());
}

And for the regex , it must do the following search:

[Any character Special] [free spaces (any quantity) containing a maximum of 1 line] [letters] ['not letter' (the first q appears ends)]

Caption:

  1. line - n
  2. space - ' '

free space - I only thought this way because I would like him to finish the regex only after seeing the first letter, but not if they came two lines or more!

Can someone convert it to regex?

  • any amount of space or line?

  • 1

    Rodrigo, I also did not understand what this "space or line" would be. I also didn’t understand if it can be "in any amount" or if it should only have one line as the title says. Could clarify these doubts and put some examples of valid and invalid values?

  • edited I hope it helps to understand. It is difficult to even explain ...

  • Put test cases that should be accepted and should be rejected.

  • From what I understand it is enough to test if there is a break. Isn’t that so? For a single line there is no break.

  • It would be something like this (anything that doesn’t have a break) : /(.*)[^\n]+/. Example, if you go all the way do this.

Show 1 more comment

1 answer

3


I’m not sure I understand, but:

([^\n]*\n)([a-zA-Z]+)([^a-zA-Z])
  • ([^\n]*\n) take anything until you find one \n
  • ([a-zA-Z]+) a-za-Z any quantity of at least 1
  • ([^a-zA-Z]) anything other than a-za-Z, once.

OBS.: \n = line breaking.

  • ([a-za-Z]+) this is not at least 1, (is 1 or more), for at least 1 use:([a-za-Z]{1,}).

  • @Ivanferrer + in REGEX is the same thing as {1,}, and * = {0,}

  • Well, this regex solved almost everything! I just switched [ n] by W to pick up only special characters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.