tags:

views:

114

answers:

4

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]$/';
+5  A: 

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.

Vinko Vrsalovic
A: 

\S matches any non-whitespace char - thus _

tanascius
+2  A: 

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.

Peter Boughton
/s lowercase means whitespace
Brabster
Almost. \s lowercase means whitespace - note the slash direction.
Peter Boughton
Whoops sorry! Funny mobile keyboard layout!
Brabster
A: 

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.

Eduardo Xavier