0

I tried so many codes that I found on internet but none of them would work.

I have a HTML code something like this.

<div class="usernameHolder">Username: user123</div>

what I want is get the text user123 from this line of code, of course this code is with the rest of the HTML content (an HTML page) Can anyone point me to the right direction?

$text = @source=~ /Username:\s+(.*)\s+</;
print $text;

but it won't return anything.

4

2 Answers 2

2

If the HTML is in a string:

$source = '<div class="usernameHolder">Username: user123</div>';

# Allow optional whitespace before or after the username value.
$text = $source=~ /Username:\s*(.*?)\s*</;

print $1 . "\n";   # user123

If the HTML is in an array:

@source = (
    '<p>Some text</p>',
    '<div class="usernameHolder">Username: user123</div>',
    '<p>More text</p>'
);
# Combine the matching array elements into a string.
$matching_lines = join "",grep(/Username:\s*(.*?)\s*</, @source);

# Extract the username value.
$text = $matching_lines =~ /Username:\s*(.*?)\s*</;

print $1 . "\n";   # user123

A more-compact version using an array:

@source = (
    '<p>Some text</p>',
    '<div class="usernameHolder">Username: user123</div>',
    '<p>More text</p>'
);

# Combine the matching array elements in a string, and extract the username value.
$text = (join "",grep(/Username:\s*(.*?)\s*</, @source)) =~ /Username:\s*(.*?)\s*</;

print $1 . "\n";   # user123
Sign up to request clarification or add additional context in comments.

Comments

2

Your second \s+ doesn't match anything, since there is no space between user123 and the following tag.

How about this?

/Username:\s*(.*?)\s*</

Here, \s* is discarding spaces if there are any, and .*? is there so that you don't grab most of the document in the process. (See greedy vs. non-greedy)

2 Comments

This answer is wrong. You cannot apply a pattern match to an array, and that's why nothing is printing. (well, you can, but then the array is put in scalar context and returning its size, not its content)
@TLP: Oh. You're right, that's an error too. I didn't even notice that, I focused on the regexp not fitting the use case. It doesn't make anything I wrote wrong, it just makes it incomplete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.