Jump to content

search for unknown number in a string?


garyed
Go to solution Solved by mac_gyver,

Recommended Posts

I want to search for a string like:  "Joe has 5 large apples" but don't know the actual number. 

In other words I want to search for the whatever number is in between "Joe has" & "large apples" 

I assume there is a way to do that in php but I haven't been able to find it. If so can someone help me with the solution ? 

Link to comment
Share on other sites

I've tried preg_match, preg_match_all & preg_quote but I can't find the syntax where I can find an unknown value in the string. 

All the examples I've found assume there are no unknowns in the string. 

In my string "Joe has 5 large apples", the number 5 is unknown during the programming call & I only know the string is "Joe has * large apples" .

 

Link to comment
Share on other sites

  • Solution
$string = 'Joe has 5 large apples';

// find any digit(s) in the string
if(preg_match('!\d+!', $string, $matches))
{
	print_r($matches); // result in $matches[0]
}
else
{
	echo 'no number found';
}

// find digit(s) with the specific before/after text
if(preg_match('!Joe has (\d+) large apples!', $string, $matches))
{
	print_r($matches); // result in $matches[1]
}
else
{
	echo 'no number found';
}

 

Link to comment
Share on other sites

Well I thought everything was solved but i found that (\d+) only works on whole numbers. 

I didn't think it would matter if the number had a few decimal points but I was obviously wrong.  

If there is a decimal like 5.5 or 5.25 then it will only pick up the 5 & not the rest of the number.  

Link to comment
Share on other sites

13 minutes ago, Barand said:

Try my solution with 5.25

Your solution would work if I knew the number in the string but the problem is that don't know the number that will be in the the string that I'm searching for.

Basically I'm searching for the number inside a specific string that is inside a larger string. 

Link to comment
Share on other sites

46 minutes ago, garyed said:

Your solution would work if I knew the number in the string but the problem is that don't know the number that will be in the the string that I'm searching for.

Bullsh*t. Have you even tried it?

Example...

$strings = [  "Joe has 5 large apples",
              "Joe has 5.25 large apples",
              "Joe has 500 large apples"
           ];

foreach ($strings as $str) {          
    $number = current( array_filter(explode(' ', $str), 'is_numeric' ) ) ;
    echo "$str | $number<br>";
}

image.png.366c291c79693a10fb8004ca21556167.png

Link to comment
Share on other sites

1 hour ago, Barand said:

Bullsh*t. Have you even tried it?

Example...

$strings = [  "Joe has 5 large apples",
              "Joe has 5.25 large apples",
              "Joe has 500 large apples"
           ];

foreach ($strings as $str) {          
    $number = current( array_filter(explode(' ', $str), 'is_numeric' ) ) ;
    echo "$str | $number<br>";
}

image.png.366c291c79693a10fb8004ca21556167.png

 

I appreciate your help but I don't think that solves the problem. I just need to find one specific unknown number in a specific string out of a huge string with multiple numbers. 

The example of "Joe has (x) large apples" would be the line I'm searching for in like 1,000 lines of code with multiple numbers where (x) is an unknown number. 

Mac_gyver's solution worked because it took the specific string where the unknown number was inside it & pulled it out. 

The only problem is that it would only pull out a whole number & the decimal part was lost. 

Your solution would come up with too many numbers that I would still have to find a way to narrow it down to the specific string that holds the correct number.   

Link to comment
Share on other sites

What you should do is spend a little time learning regular expressions, and play with them to find a working pattern.

The pattern \d matches only digits (ie, 0 - 9).  As such, it'll only match numbers the consist of simply digits (no decimals, no grouping characters).

If you want to match decimals, you'd need to add . to the list of allowed characters.  You can do that by specifying a set of possible characters using [ ].

Joe has ([\d.]+) large apples

If you needed groups, you could likewise add , to the set.

Link to comment
Share on other sites

2 hours ago, kicken said:

What you should do is spend a little time learning regular expressions, and play with them to find a working pattern.

The pattern \d matches only digits (ie, 0 - 9).  As such, it'll only match numbers the consist of simply digits (no decimals, no grouping characters).

If you want to match decimals, you'd need to add . to the list of allowed characters.  You can do that by specifying a set of possible characters using [ ].

Joe has ([\d.]+) large apples

If you needed groups, you could likewise add , to the set.

Thank you, 

That was what I needed & I definitely do need to spend a lot more time learning those expressions. 

I know it's wrong but I usually learn backwards & try to find a solution to a problem when i run into one instead of learning the basics first.  

Link to comment
Share on other sites

1 hour ago, mac_gyver said:

@garyed, this is why you must address all the actual extents and limits of a problem, upfront, so that you don't waste time on partial solutions. it's not possible to sneak up on programming, because programming is like a BS detector.

My bad, I was trying to make my example simple to get across what I was trying to do & never even considered that the answer could be different if there were decimals involved. 

Hopefully I'll be a little more thorough next time, but I really appreciate the help from you guys here.  

Link to comment
Share on other sites

9 hours ago, garyed said:

never even considered that the answer could be different if there were decimals involved

It's not just the decimals that you didn't consider / include in the problem description.  If decimals were the only thing, then Barand's solution would have still worked fine as it was ok with decimals. You also should have considered / mentioned this part from the beginning:

Quote

I just need to find one specific unknown number in a specific string out of a huge string with multiple numbers.

So you were not just looking for a number in a string, you were trying to find a specific number in a string that possibly contains multiple numbers.

 

It's good to try and simplify the problem in general, you just need to be careful not to simplify it too much.

Link to comment
Share on other sites

2 hours ago, kicken said:

It's not just the decimals that you didn't consider / include in the problem description.  If decimals were the only thing, then Barand's solution would have still worked fine as it was ok with decimals. You also should have considered / mentioned this part from the beginning:

So you were not just looking for a number in a string, you were trying to find a specific number in a string that possibly contains multiple numbers.

 

It's good to try and simplify the problem in general, you just need to be careful not to simplify it too much.

I see what you're saying now. 

Link to comment
Share on other sites

On 7/14/2022 at 9:47 PM, kicken said:

What you should do is spend a little time learning regular expressions, and play with them to find a working pattern.

The pattern \d matches only digits (ie, 0 - 9).  As such, it'll only match numbers the consist of simply digits (no decimals, no grouping characters).

If you want to match decimals, you'd need to add . to the list of allowed characters.  You can do that by specifying a set of possible characters using [ ].

Joe has ([\d.]+) large apples

If you needed groups, you could likewise add , to the set.

I have been learning & experimenting with regular expressions as you suggested & was just curious as to why you use the square brackets inside the curly brackets. 

I've found that whether I use the square brackets or not, the result is the same.  Is it optional or is there a specific reason why they should be used?

Link to comment
Share on other sites

7 hours ago, mac_gyver said:

there are multiple ways of accoupling a task. you would need to post what you tried, along with the input data that 'worked'. 

If I use either of these statements, the result is the same.

Joe has ([\d.]+) large apples
Joe has (\d.+) large apples

It doesn't seem to matter whether I put the square brackets in or not. 

Are those brackets are there for a reason beyond the scope of the usage in this particular statement?   

Link to comment
Share on other sites

there actually is a difference. try a number like .45 it won't match the (\d.+) case. this is because there isn't a numerical digit as the first character.

the [] define a character set/class, that will match any of the characters in any order, even just a . character.

without the [], there still can be any number of each character, but the first one must be a numerical digit.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.