Question: Regex too Capture certain words at start of string Ruby

Question

Regex too Capture certain words at start of string Ruby

Answers 2
Added at 2017-01-02 08:01
Tags
Question

Looking for help in writing a regex for capturing whether a particular string starts with certain strings and capture the start and remaining string. E.g Let's say the possible starts of strings are 'P', 'RO', 'RPX' and the sample string is 'PIXR' or 'ROXP' or 'RPX'. I am looking to write a regex which captures the start and trailing part of string if it starts with the given possible strings e.g 'PIXRT' =~ // outputs 'P' and 'IXRT'

Not very conversant with regexes so any help is really appreciated.

Answers
nr: #1 dodano: 2017-01-02 08:01

You may use a regex with 2 capturing groups, one capturing the known values at the start and the rest will capture the rest of the string:

rx = /\A(RPX|RO|P)(.*)/m
"PIXRT".scan(rx)
# => [P, IXRT]

See the Ruby demo

Details:

  • \A - start of string
  • (RPX|RO|P) - one of the values that must be at the start of the string (mind the order of these alternatives: the longer ones come first!)
  • (.*) - any 0+ chars up to the end of the string (m modifier will make . match line breaks, too).
nr: #2 dodano: 2017-01-02 09:01
def split_after_start_string(str, *start_strings)
  a = str.split(/(?<=\A#{start_strings.join('|')})/)
  if a.size == 2
    a
  elsif start_strings.include?(str)
    a << ''
  else
    nil
  end
end

start_strings = %w| P RO RPX |                    #=> ["P", "RO", "RPX"]

split_after_start_string('PIXR', *start_strings)  #=> ["P", "IXR"] 
split_after_start_string('IPXR', *start_strings)  #=> nil 
split_after_start_string('ROXP', *start_strings)  #=> ["RO", "XP"] 
split_after_start_string('RPX',  *start_strings)  #=> ["RPX", ""] 

The regex reads, "match one element of start_stringx at the beginning of the string in a positive lookbehind". For smart_strings in the examples, the regex is:

/(?<=\A#{start_strings.join('|')})/               #=> /(?<=\AP|RO|RPX)/
Source Show
◀ Wstecz