0

On running:

preg_split("/MapAttempt /", $subject)

on the given input string:

MapAttempt TASK_TYPE="CLEANUP" TASKID="task_201307250256_0001_m_000320" TASK_ATTEMPT_ID="attempt_201307250256_0001_m_000320_0" START_TIME="1374702854132" TRACKER_NAME="tracker_wsmex3:ip6-localhost/127\.0\.0\.1:43273" HTTP_PORT="50060" .
MapAttempt TASK_TYPE="CLEANUP" TASKID="task_201307250256_0001_m_000320" TASK_ATTEMPT_ID="attempt_201307250256_0001_m_000320_0" TASK_STATUS="SUCCESS" FINISH_TIME="1374702864491" HOSTNAME="/default-rack/wsmex3" STATE_STRING="cleanup" COUNTERS="{(FileSystemCounters)(FileSystemCounters)[(FILE_BYTES_WRITTEN)(FILE_BYTES_WRITTEN)(21559)]}{(org\.apache\.hadoop\.mapred\.Task$Counter)(Map-Reduce Framework)[(PHYSICAL_MEMORY_BYTES)(Physical memory \\(bytes\\) snapshot)(95113216)][(SPILLED_RECORDS)(Spilled Records)(0)][(CPU_MILLISECONDS)(CPU time spent \\(ms\\))(690)][(COMMITTED_HEAP_BYTES)(Total committed heap usage \\(bytes\\))(200998912)][(VIRTUAL_MEMORY_BYTES)(Virtual memory \\(bytes\\) snapshot)(1214373888)]}

I'm receiving the required arrays but also an additional empty index, why?

And also why is that running:

preg_split("/MapAttempt .* \./", $subject)

returns empty arrays?

1
  • What result are you expecting? Commented Feb 10, 2015 at 20:33

1 Answer 1

1

You have 2 questions.

The reason you get an additional empty index is because there is an empty string in front of the first MapAttempt.

preg_split('/A/', 'xAyAz');

would result in

array('x', 'y', 'z')

whereas

preg_split('/A/', 'AyAz');

results in an

array ('', 'y', 'z')

just like in the above example it gives you everything that is before the first delineator in the first element of the array - but there's nothing there, so it gives you an empty string.

For your second question, you are asking for to divide the $subject up by the following:

The string "MapAttempt" followed by a space followed by 0 or more of anything followed by a space followed by a period.

The 2nd question is a slightly more complicated example of the first. Your regular expression is designed to match the entire first line in the subject. So you are splitting based on what appears 1st in the $subject. Therefore you get what comes before the delineator (empty string since the delineator matches at the very beginning), then none of the first line prints because it is all encompassed in the delineator (just as preg_split('/A/', ...) doesn't give you the "A" in the returned array) and then it gives you everything after the delineator (the entire 2nd line).

(If this solves your problem, please mark as the answer. I'm trying to get enough reputation points so I can vote on something! Thanks...)

Sign up to request clarification or add additional context in comments.

3 Comments

+, however, I suggest learning the formatting options to improve your answer and include links to the docs.
For the second part thats exactly what I'm trying to do, but that results in arrays of empty string, why?
Well I got the answer, preg_split is inherently run on each line of input string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.