Ropstah
Ropstah

Reputation: 17794

Regular expression doesn't work as expected

How can it be that this regular expression also returns strings that have a _ underscore as their last character?

It should only return strings with alphabetical characters, mixed lower- and uppercase.

However, the regular expression returns: 'action_'

 $regEx = '/^([a-zA-Z])[a-zA-Z]*[\S]$|^([a-zA-Z])*[\S]$|^[a-zA-Z]*[\S]$/';

Upvotes: 0

Views: 190

Answers (4)

Eduardo Xavier
Eduardo Xavier

Reputation: 1540

You should show the text and what part from you want to extract from it. Regular expression shouldn't be so big like yours.

Work on small expression batches... At this size, is very difficult to help you.

Upvotes: 0

Peter Boughton
Peter Boughton

Reputation: 112160

The [\S] will match everything that is not whitespace, including underscore.


Also, your expression is very odd!

If you want a string that only contains letters, then use ^[a-zA-Z]*$ or ^[a-zA-Z]+$ (depending on if blank is allowed or not).

If you're trying to do something else, you will need to expand on what that is.

Upvotes: 2

tanascius
tanascius

Reputation: 53944

\S matches any non-whitespace char - thus _

Upvotes: 0

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340221

Because \S means "not whitespace character", \S matches _

A group should not have an underscore though, so, if you meant that, it could be that you are getting the whole match back and not just the first group.

Please show how are you using the regex to clarify that, if needed.

Upvotes: 5

Related Questions