Gopal Gopal - 4 months ago 8
Ruby Question

Need to fetch the password using Regular expression

a= <<EOF

Password

:

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

EOF


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?

Answer
▶ 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)
Comments