user1645365
user1645365

Reputation: 321

RegEx - removing preceding & trailing whitespace while matching contents

I want to try and construct a RegEx statement that will remove any pre-ceeding or trailing white spaces from a string (but leave any contained within the string untouched) while also matching a chosen format. For instance the string must cannot be any longer than 20 characters, can contain any of the characters from a-zA-Z0-9 as well as underscores and hyphens. But most importantly it must trim or ignore any spaces found at either the start or end of the string so:

Correct: "Have a nice day"

Incorrect: " Have a nice day to "

I have tried many different ways of doing this but unfortunately so far I have not been able to come up with a formula that does exactly what I want. Could anyone help me with a suitable RegEx? (this is a RegEx in its simplest form and not platform specific).

Upvotes: 4

Views: 9490

Answers (5)

Andrii Lysenko
Andrii Lysenko

Reputation: 1

Here is an example using sed

echo "  Have a nice day  "|sed -E "s/^ *([^ ]+.*[^ ]+) +$/\\1/"

Upvotes: 0

sp00m
sp00m

Reputation: 48807

This one should suit your needs:

^\S[\w\s-]{0,18}\S$

18 is the maxlength - 2 of the string, because of the two \S that will match any non whitespace char each. For example it this case, strings won't match if their length is higher than 20 chars. Moreover, strings won't match if their length is lower than 2 chars, due to the same above constraint.

Upvotes: 0

Sina Iravanian
Sina Iravanian

Reputation: 16286

It seems from your example that space is allowed in the middle of the string as well, so try this one

^((([a-zA-Z0-9\-_][a-zA-Z0-9\-_ ]{0,18}?[a-zA-Z0-9\-_]))|([a-zA-Z0-9\-_]))$

Anything which is matched is correct.

The or section in the pattern is used for supporting a single character sentence.

Upvotes: 0

burning_LEGION
burning_LEGION

Reputation: 13450

use this regex ^\S((.{0,18}\S)|)$

^ begin of string

\S non-space symbol

(.{0,18}\S)? any symbol and non-space symbol at the end (0-19 symbols)

| or

$ end of string

Upvotes: 1

Guillaume
Guillaume

Reputation: 1879

Search for this pattern:

^\s*(.*?)\s*$

Replace with this one:

\1

Upvotes: 4

Related Questions