Can someone help me to find the job id from the text below:
$search_string = '/jobs/17 has no compatible crew with sufficient capacity (job: 304390)';
I have tried the following but did not have any luck:
preg_match("/\[job: ([A-Za-z\/]+)\]/", $search_string, $match);
print_r($match) --showing empty
You should match parenthesis instead, and capture 1 or more digits. Then you can get the group 1 value:
\(job: (\d+)\)/
Example
$search_string = '/jobs/17 has no compatible crew with sufficient capacity (job: 304390)';
if (preg_match("/\(job: (\d+)\)/", $search_string, $match)) {
print_r($match[1]);
}
Output
304390
Related
I want to extract John Doe from the string \n*DRIVGo*\nVolledige naam: John Doe\nTelefoonnummer: 0612345678\nIP: 94.214.168.86\n
So I guess the regex pattern needs to extract all characters between 'Volledige naam:' and '\n'. Is there anyone who can help me out?
You may use this regex to capture the name in group 1,
naam:\s+([a-zA-Z ]+)
As the name can only contain alphabets and spaces hence use of [a-zA-Z ]+ charset.
Php sample codes,
$str = "\n*DRIVGo*\nVolledige naam: John Doe\nTelefoonnummer: 0612345678\nIP: 94.214.168.86\n";
preg_match('/naam:\s+([a-zA-Z ]+)/', $str, $matches);
print_r($matches[1]);
Prints,
John Doe
Online demo
You can use
^Volledige naam:\s*\K.+
in multiline mode. That is
^ # start of line
Volledige naam:\s*\K # Volledige naam:, whitespaces and "forget" what#s been matched
.+ # rest of the line
In PHP:
<?php
$string = <<<DATA
*DRIVGo*
Volledige naam: John Doe
Telefoonnummer: 0612345678
IP: 94.214.168.86
DATA;
$regex = '~^Volledige naam:\s*\K.+~m';
if (preg_match($regex, $string, $match)) {
print_r($match);
}
?>
See a demo on ideone.com as well as on regex101.com.
The required string exists constantly at indexOf(':') and ends at the same call using the previously obtained value of indexOf as the offset in the subsequent call. (Given that the first call doesn't indicate that the result was not found and also that result of the send call [which would indicate the complete segment is not contained in the string])
Using a regular expression for this seems less useful because the source string will not varry in some way which requires automata.
Consider a simple split('\n') operation [optionally given a length of matches to obtain] which can be followed by further such calls if necessary to obtain the desired value without the need of any underlying engine.
The logic provided would be the same as a Regex is doing for you with it's underlying implementation although the associated cost both in terms of memory and performance is usually only justified for certain scenarios [for instance involving code page or locale conversions but not limited to, another case would be finding words with incorrect Declension, Punctuation etc.] which in this case do not seem to be needed.
Consider a parser construct with fields and methods that can obtain [point to] and also verify the integrity of the data when requires; This will also allow you to quickly serialize and deserialize the results in most cases.
Finally since you indicated your language is PHP I figured I should also let you know that equivalent of indexOf is strpos and the following code will demonstrate various ways to solve this problem without the use of regex.
$str = "\n*DRIVGo*\nVolledige naam: John Doe\nTelefoonnummer: 0612345678\nIP: 94.214.168.86\n";
$search = chr(10);
$parts = explode($search, $str);
$partsCount = count($parts);
print_r($parts);
if($partsCount > 1) print($parts[1]); //*DRIVGo*
print('-----Same results via different methodology------');
$groupStart = 0;
$groupEnd = $groupStart;
$max = strlen($str);
//While the groupEnd has not approached the length of str
while($groupEnd <= $max &&
($groupStart = strpos($str, $search, $groupStart)) >= 0 && // find search in str starting at groupStart, assign result to groupStart
($groupEnd = strpos($str, $search, $groupEnd + 1)) > $groupStart) // find search in str starting at groupEnd + 1, assign result to groupEnd
{
//Show the start, end, length and resulting substring
print_r([$groupStart, $groupEnd, $groupEnd - $groupStart, substr($str, $groupStart, $groupEnd - $groupStart)]);
//advance the parsing
$groupStart = $groupEnd;
}
Hello awesome people on the internet! I need some help :)
I have a php rcon script, this script saves the result of the rcon to a variable named results, this is an example.
results = Showing 2 tracked objective(s) for lluiscab:- rcon: 4 (rcon)- test: 5555 (test)
I want to set a variable like rcon to 4 and test to 5555.
I used explode and other thinks that I found on the web, but I can't make it work. Does someone know how to do it?
Edit: This variable changes, so, sometimes I can have rcon, test and coins and sometime only rcon
You can use a regular expression for this.
preg_match('/rcon:\s*(\d+).*test:\s*(\d+)/', $line, $match);
$rcon = $match[1];
$test = $match[2];
\d+ matches a sequence of numbers, and putting () around it makes it a capture group. $match contains the parts of the input line that were matched by the regular expression, and $match[N] contains the Nth capture group.
If you need to capture anything that looks like word: number, you can use preg_match_all and an associative array.
preg_match_all('/(\w+):\s*(\d+)/', $line, $matches, PREG_SET_ORDER);
$results = array();
foreach ($matches as $match) {
$results[$match[1]] = intval($match[2]);
}
For the example input, this will create
$results = array(
'rcon' => 4,
'test' => 5555
);
DEMO
A user from other thread help me to figure out how to get the numbers from an array, but now I can't get the numbers afer "-" dash. Let me show you what I have and put you in situation.
I''ve got an array with the next content:
Array(
[0] => <tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Information</td><td>More information</td></tr>
[1] => <tr><td>12/03/2015</td><td>10:12</td><td>98545 Column information</td><td>67659 Column information - 32</td><td>Information</td><td>More information</td></tr>
[2] => <tr><td>11/02/2015</td><td>12:40</td><td>59675 Column information</td><td>94859 Column information - 11</td><td>Information</td><td>More information</td></tr>
[3] => <tr><td>01/01/2015</td><td>20:12</td><td>69365 Column information</td><td>78464 Column information - 63</td><td>Information</td><td>More information</td></tr>
)
Finally I know how to get every number (except the number after dash "-"):
$re = "/.*?(\\d+)\\s.*?(\\d+)\\s.*/m";
$str = "<tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Information</td><td>More information</td></tr>";
$subst = "$1, $2";
$result = preg_replace($re, $subst, $str);
Here's the $result; output:
foreach($result as $finalresult) echo $finalresult.'<br>';
12345,67899
98545,67659
59675,94859
69365,78464
What I expected from all this process and cannot figure out is to get the number after dash "-" too:
12345,67899-12
98545,67659-32
59675,94859-11
69365,78464-63
But this does not end here... when the number after dash "-" is lower than 50 I need to transform the $result output. See the example below.
If the number after "-" < 50 then it needs to be transformed, taking the first digit and putting it at units position. Then the tens position might be zero.
When is 50 or above, the number ramains as it is. Example:
12345,67899-12 ------> 12345,67899-01
98545,67659-32 ------> 12345,67899-03
59675,94859-11 ------> 12345,67899-01
52375,53259-49 ------> 12345,67899-04
69365,73464-63 ------> 12345,67899-63
89765,12332-51 ------> 12345,67899-51
38545,54213-70 ------> 12345,67899-70
And now is when my head explodes!
Beforehand thanks a lot for your help.
This may be what you are looking for. I modified your regular expression slightly. The (.*?<td>){3} will match anything up to the third <td>. The ?P<first> in the subpattern (?P<first>\d+) etc. is called a named subpattern, which makes their value easy to access from the $matches array.
$a = [
'<tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Information</td><td>More information</td></tr>',
'<tr><td>12/03/2015</td><td>10:12</td><td>98545 Column information</td><td>67659 Column information - 32</td><td>Information</td><td>More information</td></tr>',
'<tr><td>11/02/2015</td><td>12:40</td><td>59675 Column information</td><td>94859 Column information - 11</td><td>Information</td><td>More information</td></tr>',
'<tr><td>01/01/2015</td><td>20:12</td><td>69365 Column information</td><td>78464 Column information - 63</td><td>Information</td><td>More information</td></tr>',
];
$result = [];
foreach ($a as $row) {
$p = '#(.*?<td>){3}(?P<first>\d+).*?</td><td>(?P<second>\d+).*?(?P<third>\d+)#';
if (preg_match($p, $row, $matches)) {
if ($matches['third'] < 50) {
$matches['third'] = '0'.$matches['third'][0];
}
$result[] =
$matches['first'] . ',' .
$matches['second'] . '-' .
$matches['third'];
}
}
print_r($result);
Output:
Array
(
[0] => 12345,67899-01
[1] => 98545,67659-03
[2] => 59675,94859-01
[3] => 69365,78464-63
)
This will do the trick for you:
$re = '/.*?(\d+)\s.*?(\d+)\s.*?-\s(\d+).*/';
$str = "<tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Information</td><td>More information</td></tr>";
preg_match($re, $str, $matches);
if ($matches[3]<50) $matches[3] = floor($matches[3]/10);
$format = '%d,%d-%02d';
$result = sprintf($format, $matches[1], $matches[2], $matches[3]);
echo $result;
Note that I changed your $re to be single quoted instead of double quoted for readability, and I'm using preg_match instead of preg_replace so I can work with the matched patterns.
To explain the regex to you, there are a few things going on:
/ is the regex delimiter.
.*?: The . tells the regex to match any character. The * says to do it zero or more times, and the ? says to do it in a "lazy" fashion. The plain .* at the end of $re matches the whole rest of the string.
(\d+): The \d is a wildcard telling the regex to match any digit. The + says "one or more times", and the () says to capture this. The first () surrounded group is $matches[1].
\s: Is a wildcard for any space character.
-: Is the literal - character.
Well... I don't know if it will help, but I made this with RegExr and it fits properly:
(([0-9]+){5})|(- [0-9]{2})
I hope you might find it some use!
I am filtering database results with a query string that looks like this:
attribute=operator(value|optional value)
I'll use
$_GET['attribute'];
to get the value.
I believe the right approach is using regex to get matches on the rest.
The preferred output would be
print_r($matches);
array(
1 => operator
2 => value
3 => optional value
)
The operator will always be one word and consist of letters: like(), between(), in().
The values can be many different things including letters, numbers, spaces commas, quotation marks, etc...
I was asked where my code was failing and didn't include much code because of how poorly it worked. Based on the accepted answer, I was able to whip up a regex that almost works.
EDIT 1
$pattern = "^([^\|(]+)\(([^\|()]+)(\|*)([^\|()]*)";
Edit 2
$pattern = "^([^\|(]+)\(([^\|()]+)(\|*)([^\|()]*)"; // I thought this would work.
Edit 3
$pattern = "^([^\|(]+)\(([^\|()]+)(\|+)?([^\|()]+)?"; // this does work!
Edit 4
$pattern = "^([^\|(]+)\(([^\|()]+)(?:\|)?([^\|()]+)?"; // this gets rid of the middle matching group.
The only remaining problem is when the 2nd optional parameter does not exist, there is still an empty $matches array.
This script, with the input "operator(value|optional value)", returns the array you expect:
<?php
$attribute = $_GET['attribute'];
$result = preg_match("/^([\w ]+)\(([\w ]+)\|([\w ]*)\)$/", $attribute, $matches);
print($matches[1] . "\n");
print($matches[2] . "\n");
print($matches[3] . "\n");
?>
This assumes your "values" match [\w ] regexp (all word characters plus space), and that the | you specify is a literal |...
if(strlen(trim($steamid)) != 0)
{
$regex = "/^STEAM_0:(0|1):[0-9]{1}[0-9]{0,8}$/";
if(!ereg($regex, $ssteamid)){
echo "STEAM ID invalida";
}
}
My problem is that this isn't working as it should.
STEAM ID's have maximum of 18 characters and minimum of 17.
they always start with: STEAM_0:
Two true examples would be: STEAM_0:0:11111111 ; STEAM_0:1:1111111
And another thing is that after STEAM_0: always come an 0 or an 1 like demonstrated in the examples.
This:
$regex = "/^STEAM_0:(0|1):[0-9]{1}[0-9]{0,8}$/";
could be writter shorter as:
$regex = "/^STEAM_0:[01]:[0-9]{1,9}$/";
Since your ID is 17 or 18 chars long, adjust the regex to it:
$regex = "/^STEAM_0:[01]:[0-9]{7,8}$/";
Finally, note that ereg is deprecated as of PHP 5.3. This snippet shows your php/regex with preg_match:
<?php
$steamid = "STEAM_0:0:11111111";
if(strlen(trim($steamid)) != 0)
{
$regex = "/^STEAM_0:[01]:[0-9]{7,8}$/";
if(!preg_match($regex, $steamid))
{
echo "STEAM ID invalida.\n";
} else {
echo "STEAM ID valida.\n";
}
}
?>
If the minimum length is 17 and the max length is 18, then you guys are looking at this at the wrong way.
This should do the job for you:
$regex = "/^STEAM_0:(0|1):[0-9]{7}[0-9]?$/";
And also, when in doubt, test your regexp with some of the online regexp tools like http://www.regexplanet.com/simple/index.html
Try this:
$regex = "/^STEAM_0:(0|1):[0-9]{8,9}$/";
It matches:
STEAM_0: at the beginning, followed
by
a 0 or a 1, followed by a :,
followed by
Either 8 or 9 digits, making the
total number of characters 17 or 18
You should be avoiding use of ereg() and its related functions; they've deprecated for quite a long time in PHP. Use thepreg_xx() functions instead (preg_match() in this case).
One of the differences between preg_match() and ereg() is that ereg() does not expect you to include the slash characters to start and end the regex, whereas preg_match() does. This is probably the main reason your code isn't working: since you've included the slashes, your use of ereg() won't work, but is you switch to preg_match(), the same expression should work fine.
So a simple change to preg_match() should sort you problem. But while I'm here, I may also suggest that you can shorten your regex string quite considerably. Try something like this:
$regex = "/^STEAM_0:[01]:[0-9]{8,9}$/";
if(!preg_match($regex, $ssteamid)) {
echo "STEAM ID invalid";
}
Hope that helps.