1. Technology
Send to a Friend via Email
You can opt-out at any time. Please refer to our privacy policy for contact information.

One or More: + and +?


The preceding element will be matched one or more times. The non-greedy version will match only one version if possible. Unlike the *? quantifier, the +? quantifier does not have to be sandwiched in order to make sense. If it is the quantifier of the last element in the regex, it will simple match one of that element.

#!/usr/bin/env ruby

text = "!foo!bar!baz!"

# This will match the entire <a href="/od/glossary/g/string.htm">string</a>
# and replace it with "X"
puts text.sub( /.+!/, "X" )

# This will match only !foo! and print
# "Xbar!baz!"
puts text.sub( /.+?!/, "X" )
  1. About.com
  2. Technology
  3. Ruby
  4. Regular Expressions
  5. One or More: + and +?

©2014 About.com. All rights reserved.