[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

fetching variables from text

The format of Perl is very confusing.

I have the script to get the variables 'username1' and 'password1' from a text file with the following format:

'<b>Username: username1</b> and <b>Password: password1</b>'

Using the script (snippet):

($username) = $page =~ /<b>Username:\s(.*?)<\/b>/i;
($password) = $page =~ /<b>Password:\s(.*?)<\/b>/i;

How do I format this script to get the variables 'username1' and 'password1' from text formatted in this way?  (I've tried everything I can think of):

'Username: </b>username1&nbsp;&nbsp;&nbsp;
<b>Password:</b> password1<br>'

***Note that there is a space between 'Username"' and '</b>' and also a space between '</b>' and 'password1'

The formatting of the script is very confusing.  What do all the backslashes and (\s)'s mean?  Could someone help me to understand the way this is formatted?  Thanks.


1 Solution
Username: </b>username1&nbsp;&nbsp;&nbsp;
<b>Password:</b> password1<br>

Try this:
($username) = $page =~ /Username:\s{0,}<\/b>\s{0,}(.*?)\&/i;
($password) = $page =~ /<b>Password:\s{0,}<\/b>\s{0,}(.*?)<br>/i;

Now, what does this mean. In the first line you search for the string "Username:" followed by 0 or more whitespaces "\s{0,}". Then must follow "</b>" which is written as "<\/b>" because only at the end "/" may appear. Then you look again for 0 or more whitespaces. Finally you look for the password "(.*?)". "." means any character "*" means any number of characters and "?" means minimum matching. All three characters are enclosed with "()" to tell the program to store the value in $username. Minimum matching means that username will be taken to the first "&" character. If "?" was omitted the username would be taken to the last "&" character. The matching for password is almost identical.
GorGor1Author Commented:

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now