I have big string in that I need to check if number is present which is more than 3.
Means "some string2" will be invalid , but "some string 3","some string7" will be correct.
preg_match('/some\s*string\s*([3-9][0-9]*|[1-9][0-9]+)/i', $haystack);
And here the working example
But, after examining your use-case, which seems to be checking for a specific version in an application description, I too would advise you to just get the number out of the string and compare it to an actual number to be sure it's larger or equal than 3:
preg_match('/([0-9]+)/', $string, $matches);
if ($matches[1] >= 3) {
// Do something
}
Regex is for text matching, not arithmetic. Right tool for the right job...
preg_match('/([0-9]+)/', $string, $matches);
if ($matches[1] >= 3) {
// Do something
}
You match a word followed by an optional space and then the number greater than 2. Thanks to the decimal places you can control that:
(\w*\s*(?:[1-9]\d+|[3-9]))
Some little example (demo):
$subject = 'I have big string in that I need to check if number is present which is more than 3.
Means "some string2" will be invalid , but "some string 3","some string7" will be correct.';
$pattern = '(\w*\s*(?:[1-9]\d+|[3-9]))';
$r = preg_match_all($pattern, $subject, $matches);
var_dump($matches);
Output:
array(1) {
[0]=>
array(3) {
[0]=>
string(6) "than 3"
[1]=>
string(8) "string 3"
[2]=>
string(7) "string7"
}
}
I hope this is helpful.
I modified Florian's solution:
[a-z]+\s?[a-z]+\s?([1-9][0-9]+|[3-9])
http://regexr.com?31ja1
It works for any string and not just "some string" and it allows only 0 or 1 whitespace character.
This wouldn't work?
$numberBiggerThanThree = preg_match('/([0-9]{2,}|[3-9])/', 'some long string 3');
Related
$lines ="do you want to cut paper here?";
$pattern = "do you '/(.*) to (.*)/' paper here?";
$matches = array();
preg_match($pattern, $lines, $matches);
var_dump($matches);
I got an error , I think the syntax i use was incorrect, I tried many similar code but it's not meet my requirement. Can anyone please suggest how to get the word that matches ?
in this example $matches[1] should be want and $matches[2] should be cut
thank you in advance
If I understand what you want correctly, this is working regex:
<?php
$lines ="do you want to cut paper here?";
$pattern = "/do you ([^\s]+) to ([^\s]+) paper here\?/";
preg_match($pattern, $lines, $matches);
var_dump($matches);
?>
it prints this result:
array(3) {
[0]=> string(30) "do you want to cut paper here?"
[1]=> string(4) "want"
[2]=> string(3) "cut"
}
I agree with mario, sscanf() is a better choice. sscanf() doesn't create a "fullstring match" like preg_match() does, in other words it doesn't make more data than is required.
Also, you don't need to match the entire string, just the string up to the final word to be extracted.
%s in the format string means one or more continuous non-whitespace characters.
Code: (Demo)
$string = "do you want to cut paper here?";
$format = "do you %s to %s";
var_export(sscanf($string, $format));
Output:
array (
0 => 'want',
1 => 'cut',
)
I am trying to find a php preg_match that can match:
"2-20 to 2-25"
from this text:
user levels 2-20 to 2-25 not ready
I tried
preg_match("/([0-9]+) to ([0-9]+)/", $vars[1] , $matchesto);
but the result is:
"20 to 2"
Any help appreciated.
Your pattern is almost correct; just include the dashes and adjust the capture group:
([-0-9]+ to [-0-9]+)
Example:
https://regex101.com/r/eD6lQ2/1
Thats because [0-9]+ matches one or more numbers but won't match a hyphen (-).
Try this:
$pattern = '~([0-9]+-[0-9]+) to ([0-9]+-[0-9]+)~Ui';
preg_match($pattern, $vars[1] , $matchesto);
You can use "\d" to match the digits:
<?php
$str = 'user levels 2-20 to 2-25 not ready';
$matches = array();
preg_match('/(\d+-\d+) to (\d+-\d+)/', $str, $matches);
var_dump($matches);
Output:
array(3) {
[0]=>
string(12) "2-20 to 2-25"
[1]=>
string(4) "2-20"
[2]=>
string(4) "2-25"
}
I have a sentence (sometimes long, sometimes not) which has to be exploded into a string. The catch is, that it has to be separated by length (never cutting off words).
Example:
$sentence = 'I slipped it into that Stevia crap that you're always putting in your tea.';
Should be (using 15 letters top per line):
$array[0] = 'I slipped it into';
$array[1] = 'that Stevia crap';
$array[2] = "that you're";
$array[3] = 'always putting';
$array[4] = 'in your tea.';
As you can see, some lines like [1] really should be cut in that Stevia cr but since the cut is in the middle of the last word, it is still allowed to be included in this chunk.
My current approach is to cut the $sentence by chunks of the same size, but that does cut some words. Any ideas will help. Thanks!
you can use the wordwrap function php manual
$text = "This is some text which we will use for testing";
$newtext[] = explode(":", wordwrap( $text, 18, ":" ));
var_dump($newtext);
the output would be:
array(1) {
[0]=> array(3) {
[0]=> string(17) "This is some text"
[1]=> string(17) "which we will use"
[2]=> string(11) "for testing"
}
}
Is there a way to get the match patterns to change order? For example if you have a string with letters-digits and using preg_match_all(), and you want the resulting match array to have the digits before the letters. Is there a way to specify this in the regular expression itself?
So "aaa-111" would result in matches with
array(0 => '111', 1 => 'aaa');
Perhaps named capture groups will help. Example:
preg_match('/(?<alphapart>[a-z]+)-(?<numpart>[0-9]+)/', 'aaa-111', $matches);
$matches:
array('alphapart' => 'asd', 'numpart' => '111')
This way you can refer to the matches by a name instead of whatever order index they were matched in.
Edit: Just for accuracy, I want to note that $matches will actually include the matches by index as well, so the actual $matches will be: array(5) { [0]=> string(7) "aaa-111" ["alphapart"]=> string(3) "aaa" [1]=> string(3) "aaa" ["numpart"]=> string(3) "111" [2]=> string(3) "111" }
The order of groups in a regex is dependent on their positions in the regex and the string. Changing the order would make it very confusing.
What you can do is use "named groups".
/(?P<letters>\w*)-(?P<digits>\d*)/
The array will still be in the same order, but, you can use $matches['digits'] to easily get just the digits.
DEMO: http://ideone.com/3tRJLZ
Yes you can. You can use lookaheads that don't push the 'cursor' and so you could first match the last part, and then the first part. It works with (?=regex)
This works:
(?=\w+\-(\d+))(\w+)\-\d+
but will also give the full match at index 0. Like ["aaa-111", "111", "aaa"]
is that a problem?
I don't believe there is. Regex isn't designed to sort. You could setup two different regular expressions to check for each pattern though. This code will echo the two string in num/alpha order as you requested:
<?php
header('Content-Type: text/plain');
$string1 = 'aaa-123';
$string2 = '123-aaa';
echo 'String 1: '.$string1."\n";
echo 'String 2: '.$string2."\n";
$pattern1 = '/([\d]+)-([a-z]+)/i';
$pattern2 = '/([a-z]+)-([\d]+)/i';
echo 'Result 1: ';
if(preg_match($pattern1, $string1, $matches))
{
echo $matches[1].' '.$matches[2]."\n";
}
if(preg_match($pattern2, $string1, $matches))
{
echo $matches[2].' '.$matches[1]."\n";
}
echo 'Result 2: ';
if(preg_match($pattern1, $string2, $matches))
{
echo $matches[1].' '.$matches[2]."\n";
}
if(preg_match($pattern2, $string2, $matches))
{
echo $matches[2].' '.$matches[1]."\n";
}
?>
The resulting output is:
String 1: aaa-123
String 2: 123-aaa
Result 1: 123 aaa
Result 2: 123 aaa
If you want the digits to be there first, you need to sort the array yourself.
array_sort() will... sort it out for you.
I have for example such string - "7-th Road" or "7th number some other words" or "Some word 8-th word".
I need to get the first occurrence of number and all other next symbols to first occurrence of space.
So for examples above i need such values "7-th", "7th", "8-th".
And then from these matches like "7-th" i need extract only numbers in other operations.
Thanks in advance!
Regex should be /(\d+)([^\d]+)\s/ and the numbers would resolve to $1 and the ending characters to $2
Sample Code:
$string = '7-th Road';
preg_match_all('/(\d+)([^\d]+)\s/', $string, $result, PREG_PATTERN_ORDER);
var_dump($result[1]);
array(1) {
[0]=> string(1) "7"
}
var_dump($result[2]);
array(1) {
[0]=> string(1) "-th"
}
Are you asking for something like this?
#(\d+)-?(?:st|nd|rd|th)#
Example
If you would like to get just nums from the text use it:
preg_match_all('/(\d+)[th|\-th]*?/','7-th", "7th", "8-th', $matches);
But if you would like to remove 'th' or other just do replacement:
preg_replace('/(\d+)[th|\-th]*?/','$1', 'some string')
Not sure about the last one...