HolyMonk
HolyMonk

Reputation: 452

Regex look for string which may contain whitespaces

I have a string of the form:

absdaskk adknksadn daksnksa > words words words >

I would like to have a pattern which returns me ''words words words''. I'm sure I can do this using a regular expression, I tried using:

"&gt [A-Za-z\s] &gt"

but this gives me no results, I'm pretty sure the problem is with the whitespaces, which I think are represented by \s, but probably aren't..

Note: I may not use something like split, as the full string may contain more &gt parts, which is unpredictable.

Upvotes: 0

Views: 565

Answers (2)

Raman Sahasi
Raman Sahasi

Reputation: 31901

To find whitespace, you can either use \s as well as literally a blank space , both works fine so there's no space issue in your code.

The problem is [A-Za-z\s] is only referring to 1 character which can be either A-Z, a-z or \s, but we need to refer to zero or more. So we need a * after these like this:

[A-Za-z\s]*

also, you forgot a semicolon after &gt. Note that if you are referring to content between >, then to get them, you need capture group.

>([A-Za-z\s]*)>

Upvotes: 0

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27271

You forgot the ;. Also, you need to throw in a + to ensure more than one character is matched.

> [A-Za-z\s]+ >

Upvotes: 1

Related Questions