user288609
user288609

Reputation: 13025

regular expression to match this type of string

I want to find the strings that share the following pattern:

The first character is + and the last character is a space. There can have more than one characters between these two characters. They can be a digit or a digit or a letter, or any other character, such as *, &, etc. But they can not be space. In other words, there only has one space for this string and is at the end position.

How to represent this pattern using regular expression?

Upvotes: 1

Views: 65

Answers (2)

codaddict
codaddict

Reputation: 455020

You can try the regex

\+[^ ]+ 

There is a space at the end of the above regex.

See it

Upvotes: 0

Michael Mrozek
Michael Mrozek

Reputation: 175375

^ matches the beginning of a string, and $ the end. You can make a character class with [] that matches only the things in the class, and ^ at the beginning makes it match anything not in the class, so [^ ] means "anything except a space". So the complete match is:

^\+[^ ]* $

Upvotes: 5

Related Questions