Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9091753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:41:17+00:00 2026-06-16T22:41:17+00:00

I have a text file with multiple occurrences of tables like show below: _____________________________________

  • 0

I have a text file with multiple occurrences of tables like show below:

_____________________________________
Heading 1       | Heading 2
_______________ | ___________________
Label1 18857.10 | Label3 710.00
Label2 2361.50  | Label4 0.00
                | Label5 2531.37
                | Label6 0.00
                | Label7 0.00
                | Label8 0.01
________________| ___________________
       16495.60 | Label9 3969.06
_______________ | ___________________

I want to store the numerical values into variables using regular expressions. Since I’m new to regular expressions, I couldn’t find a way to do it. Can anyone help me with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:41:17+00:00Added an answer on June 16, 2026 at 10:41 pm
    $table="_____________________________________
    Heading 1       | Heading 2
    _______________ | ___________________
    Label1 18857.10 | Label3 710.00
    Label2 2361.50  | Label4 0.00
                    | Label5 2531.37
                    | Label6 0.00
                    | Label7 0.00
                    | Label8 0.01
    ________________| ___________________
           16495.60 | Label9 3969.06
    _______________ | ___________________
    ";
    
    $num = preg_match_all('/(\w+) (\d+(\.\d+)?)/', $table, $result);
    
    
    for($i=0; $i<$num; $i++){
      echo "{$result[1][$i]} = {$result[2][$i]}<br>";
    }
    

    If your table is exactly what you showed, this works.

    regex: /(\w+) (\d+(\.\d+)?)/

    Slashes / at the begining and end are delimiting the regex.

    (\w+) means, “match any letter,number or underscore once or more times

    one space follows, you can add + after the space, to match more then one, or put \s instead of space, to match any white character, like tab for example..

    (\d+(\.\d+)?) … \d+ means one or more digits, (\.\d+) means dot followed by one or more digits, question mark means that the previous parenthesis (\.\d+) is optional.

    Preg_match_all stores those matches in third parameter and returns number of matches. In $result[$i][0] is the whole match, $result[$i][1] is first sub-expression (\w+), $result[$i][2] is second parenthesis (\d+(\.\d+)?), $result[$i][3] is the decimal part (\.\d+), it is inside $result[$i][2], but you don’t need $result[$i][3], just for explanation 🙂

    The code prints:

    Heading = 1
    Heading = 2
    Label1 = 18857.10
    Label3 = 710.00
    Label2 = 2361.50
    Label4 = 0.00
    Label5 = 2531.37
    Label6 = 0.00
    Label7 = 0.00
    Label8 = 0.01
    Label9 = 3969.06
    

    edit: sorry, it doesn’t work, it didn’t match that naked 16495.60 value. Let me think a bit more…

    …

    $regex='/([a-zA-Z0-9]+)? +(\d+(\.\d+)?)/';
    

    is bit better, here’s how it works:

    • [a-zA-Z0-9]+ matches non-zero ammount of letters or numbers
    • ? after parenthesis means, the whole parenthesis expression is optional.
    • + one or more spaces
    • (\d+(\.\d+)?) non-zero ammount of digits followed by optional { dot and another non-zero ammount of digits }

    This whole regex does not include | or new-line, so all matching should happen in only one field of the table.

    The result variable should be:

    array (size=4)
      0 => 
        array (size=12)
          0 => string 'Heading 1' (length=9)
          1 => string 'Heading 2' (length=9)
          2 => string 'Label1 18857.10' (length=15)
          3 => string 'Label3 710.00' (length=13)
          4 => string 'Label2 2361.50' (length=14)
          5 => string 'Label4 0.00' (length=11)
          6 => string 'Label5 2531.37' (length=14)
          7 => string 'Label6 0.00' (length=11)
          8 => string 'Label7 0.00' (length=11)
          9 => string 'Label8 0.01' (length=11)
          10 => string '           16495.60' (length=19)
          11 => string 'Label9 3969.06' (length=14)
      1 => 
        array (size=12)
          0 => string 'Heading' (length=7)
          1 => string 'Heading' (length=7)
          2 => string 'Label1' (length=6)
          3 => string 'Label3' (length=6)
          4 => string 'Label2' (length=6)
          5 => string 'Label4' (length=6)
          6 => string 'Label5' (length=6)
          7 => string 'Label6' (length=6)
          8 => string 'Label7' (length=6)
          9 => string 'Label8' (length=6)
          10 => string '' (length=0)
          11 => string 'Label9' (length=6)
      2 => 
        array (size=12)
          0 => string '1' (length=1)
          1 => string '2' (length=1)
          2 => string '18857.10' (length=8)
          3 => string '710.00' (length=6)
          4 => string '2361.50' (length=7)
          5 => string '0.00' (length=4)
          6 => string '2531.37' (length=7)
          7 => string '0.00' (length=4)
          8 => string '0.00' (length=4)
          9 => string '0.01' (length=4)
          10 => string '16495.60' (length=8)
          11 => string '3969.06' (length=7)
      3 => 
        array (size=12)
          0 => string '' (length=0)
          1 => string '' (length=0)
          2 => string '.10' (length=3)
          3 => string '.00' (length=3)
          4 => string '.50' (length=3)
          5 => string '.00' (length=3)
          6 => string '.37' (length=3)
          7 => string '.00' (length=3)
          8 => string '.00' (length=3)
          9 => string '.01' (length=3)
          10 => string '.60' (length=3)
          11 => string '.06' (length=3)
    

    edit2: GRAB THOSE SNIPPETS AGAIN! There should be a backslash before the dot, in (\.\d+)!!! I formated it wrong and it disappeared.** Rewrote it, should be fine now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge text file that I would like to split into multiple
I have a text file with multiple records each with a new line like
I have text file with some stuff that i would like to put into
I have text file with something like first line line nr 2 line three
I have text file, like FILED AS OF DATE: 20090209 DATE AS OF CHANGE:
I have text file with entries like 123 112 3333 44 2 How to
I' ve got a problem with Haskell. I have text file looking like this:
I have a text file and I would like to parse it using regular
I have a text file which I'm trying to parse. The file looks like
I have a text file that contains a data like ID Name Path IsTrue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.