Gopal Gopal - 8 months ago 18
Ruby Question

Need to fetch the password using Regular expression

a= <<EOF



7UV1ceFQ (You will be asked to change this after logging in for the first time)


I need to extract the value "7UV1ceFQ" using regular expression, I have tried using '/Password : 7UV1ceFQ/ but it's not working, I think it's because next line character is included, Can anyone please suggest me to exact this value?

▶ a[/^\S+(?=\s\(You will be)/]
#⇒ "7UV1ceFQ"

The regular expression above reads as:

  • starting with a new line start ^
  • get all non-space symbols greedy \S+
  • until the positive lookahead (?=\s\(You will be)