-4

I have below test case

      hello how are you     // 1. Allow  
  hello   how are you       // 2. Not Allow  
    hello <                 // 3. Not Allow  

for the following Rules:

  1. Allow spaces at start and end
  2. Not allow more than one space between words
  3. Not allow angle brackets < >

I am trying the below:

^([^<> ]+ )+[^<> ]+$|^[^<> ]+$

This is working, but when giving spaces at start or end it is not allowing.

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Md. Parvez Alam
  • 4,326
  • 5
  • 48
  • 108
  • What is the rule? If you want to match only word chars and spaces in between optional dots, you can use `/^\.*[\w\s]+\.*$/` ([demo](https://regex101.com/r/KS2J6B/1)). However, there may be other variations, like `/^\.*\s*\w[\w\s]*\.*$/`, etc. Everything depends on what you actually need. – Wiktor Stribiżew Sep 27 '18 at 10:45
  • 5
    What is your question? what you want to achieve ? – Zakaria Acharki Sep 27 '18 at 10:46
  • Try `/^\s*\w+(?:\s\w+)*\s*$/` ([demo](https://regex101.com/r/KS2J6B/2)). What do you mean by a "word"? `\w` may need replacing with `[a-zA-Z]` or something else if you also need to match all Unicode letters – Wiktor Stribiżew Sep 27 '18 at 10:50
  • It sounds as if you are trying to parse HTML now, please see [Parse an HTML string with JS](https://stackoverflow.com/questions/10585029/parse-an-html-string-with-js). – Wiktor Stribiżew Sep 27 '18 at 10:55
  • sorry * was just to bold\ – Md. Parvez Alam Sep 27 '18 at 10:59

1 Answers1

0

I assume that you use your regex to find matches in the whole text string (all 3 lines together).

I see also that both your alternatives contain starting ^ and ending $, so you want to limit the match to a single line and probably use m regex option.

Note that [^...]+ expression matches a sequence of characters other than placed between brackets, but it does not exclude \n chars, what is probably not what you want.

So, maybe you should add \n in each [^...]+ expression.

The regex extended such a way is:

^([^<> \n]+ )+[^<> \n]+$|^[^<> \n]+$

and it matches line 1 and 2.

But note that the first alternative alone (^([^<> \n]+ )+[^<> \n]+$) also does the job.

It you realy want that line 2 should not match, please specify why.

Edit

To allow any number of spaces at the begin / end of each line, add * after initial ^ and before final $, so that the regex (first alternative only) becomes:

^ *([^<> \n]+ )+[^<> \n]+ *$

But it still matches line 2.

Or maybe dots in your test string are actually spaces, but you wrote the string using dots, to show the numer of spaces? You should have mentioned it in your question.

Edit 2

Yet another possibility, allowing dots in place of spaces:

^[ .]*((?:[^<> .\n]+[ .])+[^<> .\n]+|[^<> .\n]+)[ .]*$

Details:

  • ^[ .]* - Start of a line + a sequence of spaces or dots, may be empty.
  • ( - Start of the capturing group - container for both alternatives of stripped content.
    • (?:[^<> .\n]+[ .])+ - Alternative 1: A sequence of "allowed" chars ("word") + a single space or dot (before the next "word", repeated a few times. This group does not need to be a capturing one, so I put ?:.
    • [^<> .\n]+ - A sequence of "allowed" chars - the last "word".
  • | - Or.
    • [^<> .\n]+ - Alternative 2: A single "word".
  • ) - End of the capturing group.
  • [ .]*$ - The final (possibly empty) sequence of spaces / dots + the end of line.

Of course, with m option.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41