1

I have variable strings like the below:
The.Test.String.A01Y18.123h.WIB-DI.DO5.1.K.314-ECO
The.Regex.F05P78.123h.WIB-DI.DO5.1.K.314-EYT
Word.C05F78.342T.DSW-RF.EF5.2.F.342-DDF

I would like to extract this part of these string in PHP dynamically and i was looking at using regex but haven't had much success:
The.Test.String.A01Y18
The.Regex.F05P78
Word.C05F78

And ultimately to:
The Test String A01Y18
The Regex F05P78
Word C05F78

The first part of the text will be variable in length and will separate each word with a period. The next part will always be the same length with the pattern:
One letter, 2 number, one letter, 2 numbers pattern (C05F78)

Any thing in the string after that is what I would like to remove.

2
  • 5
    Share your attempts so far. Commented Apr 1, 2014 at 23:26
  • Well it doesn't seem first part of the string. Commented Apr 1, 2014 at 23:27

3 Answers 3

1

that's it

$x=array(
    "The.Test.String.A01Y18.123h.WIB-DI.DO5.1.K.314-ECO",
    "The.Regex.F05P78.123h.WIB-DI.DO5.1.K.314-EYT",
    "Word.C05F78.342T.DSW-RF.EF5.2.F.342-DDF"
);

for ($i=0, $tmp_count=count($x); $i<$tmp_count; ++$i) {
    echo str_replace(".", " ", preg_replace("/^(.+?)([a-z]{1}[0-9]{2}[a-z]{1}[0-9]{2})\..+$/i", "\\1\\2", $x[$i]))."<br />";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Using this regular expression should work, replacing each of your strings with the first capturing group:

^((?:\w+\.)+\w\d{2}\w\d{2}).*

See demo at http://regex101.com/r/fR3pM6

Comments

1

This is valid too:

preg_match("\.*[\w\d]{6}", stringVariable)

.* for all digits atleast we found a composition of letters and words of 6 characters ([\w\d]{6})

Result:

Match 1:    The.Test.Stsrisng.A01Y18    
Match 2:    The.Regex.F05P78       
Match 3:    Word.C05F78  

Comments

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.