I try to figure out how to match following values via preg_match using:
^[\S].*[\S]{3,10}$
Unfortunately, the min works from size of 4 and the max of 10 is being ignored at all as the pattern still machtes on a lenght of 11.
Disallow leading and trailing spaces
Allow any character inside
Allow space within characters
Enforce min of 3 and max of 10 (not working)
Testset that could be used with: https://www.phpliveregex.com
[
[
"Test",
true
],
[
"Test Test",
true
],
[
"Test-Test",
true
],
[
"Test'Test",
true
],
[
"Test,Test",
true
],
[
null,
false
],
[
" ",
false
],
[
" Test ",
false
],
[
"12",
false
],
[
"12345678901",
false
]
]
Thanks for your help in advanced
You may use
^(?=.{4,10}$)\S.*\S$
See regex demo
Details
^ - start of string
(?=.{4,10}$) - four to ten chars other than line break chars up to the end of string allowed
\S - a non-whitespace char
.* - 0 or more chars other than line break chars as many as possible
\S - a non-whitespace char
$ - end of string.
Related
I have string :
$string = 'ABCCDF[GH]IJJ[KLM]';
How to i get string 'GH' and 'KLM' in php
I think i will use preg_split, but I know not more about reg. Plz help me.
Try this;
>>> $string = 'ABCCDF[GH]IJJ[KLM]';
=> "ABCCDF[GH]IJJ[KLM]"
>>> preg_match_all('/\[(\w+)\]/', $string, $matches);
=> 2
$matches will look like below.
[
[
"[GH]",
"[KLM]",
],
[
"GH",
"KLM",
],
]
I have a problem, I would like to ask for this string:
[NAME: abc] [EMAIL: email#gm.com] [TIMEFRAME: 3 weeks] [BUDGET: 1000 dollars] [MESSAGE: bla bla bla]
Replace it with an array in the form:
array(
'NAME' => 'abc',
'EMAIL' => 'email#gm.com',
'TIMEFRAME' => '3 weeks',
'BUDGET' => '1000 dollars',
'MESSAGE' => 'bla bla bla' );
I tried to do something like this:
$content = str_replace(array('[', ']'), '', '[NAME: abc] [EMAIL: email#gm.com] [TIMEFRAME: 3 weeks] [BUDGET: 1000 dollars] [MESSAGE: bla bla bla]');
preg_match_all('/[A-Z]+\:/', $content, $inputs);
I managed to pull out the "keys", but I do not know how to pull out their "values". Any ideas?
Thank you in advance for your help and I apologize for my English.
You may use the following regex:
'~\[(\w+):\s*([^][]*)]~'
See the regex demo.
Details
\[ - a [ char
(\w+) - Group 1: 1+ letters, digits or _
: - a colon
\s* - 0+whitespaces
([^][]*) - Group 2: 0+ chars other than [ and ]
] - a ] char.
See the PHP demo:
$s = "[NAME: abc] [EMAIL: cde] [TIMEFRAME: efg] [BUDGET: hij] [MESSAGE: klm]";
if (preg_match_all('~\[(\w+):\s*([^][]*)]~', $s, $m)) {
array_shift($m); // Removes whole match values from array
print_r(array_combine($m[0], $m[1])); // Build the result with keys (Group 1) and values (Group 2)
}
I'm trying to parse in json a txt file content. This is the file content:
[19-02-2016 16:48:45.505547] [info] System done.
0: array(
'ID' => 'Example 2'
)
Now this is my code for parse the file:
$fh = fopen($file, "r");
$content = array();
$content["trace"] = array();
while ($line = fgets($fh))
{
$raw = preg_split("/[\[\]]/", $line);
$entry = array();
$entry["date"] = trim($raw[1]);
$entry["type"] = trim($raw[3]);
$entry["message"] = trim($raw[4]);
$content["trace"][] = $entry;
}
fclose($fh);
return $content;
and this is what is returned from $content:
{
"trace": [{
"date": "19-02-2016 16:48:45.505547"
"type": "info"
"message": "System done."
}, {
"date": ""
"type": ""
"message": ""
}, {
"date": ""
"type": ""
"message": ""
}, {
"date": ""
"type": ""
"message": ""
}]
}
UPDATE I'm expecting this:
{
"trace": [{
"date": "19-02-2016 16:48:45.505547"
"type": "info"
"message": "System done."
"ID": Example 2
}]
}
how you can see the array is saw as a new line and the code create other empty array in the while without content. I just want create new index later message and put the array content, how I can achieve this?
UPDATE WITH MORE CONTENT IN FILE
[19-02-2016 16:57:17.104504] [info] system done.
0: array(
'ID' => 'john foo'
)
[19-02-2016 16:57:17.110482] [info] transaction done.
0: array(
'ID' => 'john foo'
)
Expected result:
{
"trace": [20]
0: {
"date": "19-02-2016 16:57:17.104504"
"type": "info"
"message": "system done."
"ID": john foo
}
1: {
"date": "19-02-2016 16:57:17.110482"
"type": "info"
"message": "transaction done."
"ID": john foo
}
...
Try this:
Code
<?php
$file = 'test.log';
$content = array();
$content["trace"] = array();
$input = file_get_contents('test.log');
preg_match_all('/\[(.*)\][\s]*?\[(.*?)\][\s]*?(.*)[\s][^\']*\'ID\'[ ]*=>[ ]*\'(.*)\'/', $input, $regs, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($regs[0]); $i++) {
$content['trace'][] = array(
'date' => $regs[1][$i],
'type' => trim($regs[2][$i]),
'message' => trim($regs[3][$i]),
'ID' => trim($regs[4][$i]),
);
}
// return $content;
echo '<pre>'; print_r($content); echo '</pre>'; // For testing only
$content = json_encode($content); // For testing only
echo '<pre>' . $content . '</pre>'; // For testing only
Result
PHP array:
Array
(
[trace] => Array
(
[0] => Array
(
[date] => 19-02-2016 16:57:17.104504
[type] => info
[message] => system done.
[ID] => john foo
)
[1] => Array
(
[date] => 19-02-2016 16:57:17.110482
[type] => info
[message] => transaction done.
[ID] => john foo
)
)
)
Json object (string):
{
"trace":[
{
"date":"19-02-2016 16:57:17.104504",
"type":"info",
"message":"system done.",
"ID":"john foo"
},
{
"date":"19-02-2016 16:57:17.110482",
"type":"info",
"message":"transaction done.",
"ID":"john foo"
}
]
}
Notes re. the RegEx:
The file is read as a whole into a string variable ($input).
The preg_match_all(RegEx) also scans the entire input.
The code iterates over all its hits, where the groups contain these parts…
1: date
2: type
3: message
4: ID
The RegEx in detail:
\[ Match the character “[” literally
( Match the regular expression below and capture its match into backreference number 1
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\] Match the character “]” literally
[\s] Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
*? Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\[ Match the character “[” literally
( Match the regular expression below and capture its match into backreference number 2
. Match any single character that is not a line break character
*? Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
\] Match the character “]” literally
[\s] Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
*? Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
( Match the regular expression below and capture its match into backreference number 3
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
[\s] Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
[^'] Match any character that is NOT a “'”
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
'ID' Match the characters “'ID'” literally
[ ] Match the character “ ”
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
=> Match the characters “=>” literally
[ ] Match the character “ ”
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
' Match the character “'” literally
( Match the regular expression below and capture its match into backreference number 4
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
' Match the character “'” literally
I am so bad at creating regex and I'm struggling with what I am SURE it's a simple stupid regex.
I am using PHP to do this match. Here is what I have until now.
Test string: 8848842356063003
if(!preg_match('/^[0-2]|[7-9]{16}/', $token)) {
return array('status' => 'failed', 'message' => "Invalid token", 'token' => '');
}
The regex must comply to this: Start with 0-2 or 7-9 and have EXACTLY 16 characters. What am I doing wrong? Because I get, as a match:
array(
0 => 8
)
And I should get:
array(
0 => 8848842356063003
)
By the way: I am using PHP Live Regex to test my regex string.
Thanks in advance,
Ares D.
The regex must comply to this: Start with 0-2 or 7-9 and have EXACTLY 16 characters
You can put starting numbers in same character class and use end anchor after matching 15 more charaters:
/^[0-27-9].{15}$/
If you want to match only digits then use:
/^[0-27-9]\d{15}$/
So, I have a text field that can contain only letters, numbers, hyphens, dots and underscores. I would like to validate it using Zend_Validate_Regex but this pattern does not work. Why?
/[a-z][A-Z][0-9]-_./
Here is my text element:
$titleSlug = new Zend_Form_Element_Text('title_slug', array(
'label' => 'Title Slug',
'required' => FALSE,
'filters' => array(
'StringTrim',
'Null'
),
'validators' => array(
array('StringLength', FALSE, array(3, 255)),
array('Regex', FALSE, array('pattern' => '/[a-z][A-Z][0-9]-_./'))
)
));
Your regex matches a string that contains a lowercase letter, an uppercase letter, a digit, a dash, an underscore and any other character, in that order. You need this:
/^[\w.-]*$/
^ and $ anchor the match at the start and end of the string.
\w matches letters, digits and underscore; together with the dot and dash they form a character class ([...]) which is repeated zero or more times (*).
how about this:
/[a-zA-Z]*|\d*|-*|\.*|_*/