I want to split number and letters from string but have problem .
Inputs like:
input example 1 : A5
input example 2 : C16
input example 3 : A725
input example 4 : X05
Result must be:
Result example 1 :'A','5'
Result example 2 : 'C','16'
Result example 3 : 'A','725'
Result example 4 : 'X','05'
I try to it with belo regex but don't give a good result :
preg_split('/(?=\d+)/', $input)
You also need to add a negative look-behind to make sure the empty string that is chosen is not somewhere in the middle of two digits.
Currently for string A725, your regex will split on the empty string before 7, 2 and 5, as all of them are followed by at least one digit.
You can use this regex:
preg_split('/(?<!\d)(?=\d+)/', $input)
You can use:
$s = 'A5,C16,A725,X05';
if (preg_match_all("~(?>[a-z]+|\d+)~i", $s, $arr))
var_dump($arr[0]);
gives:
array(8) {
[0]=>
string(1) "A"
[1]=>
string(1) "5"
[2]=>
string(1) "C"
[3]=>
string(2) "16"
[4]=>
string(1) "A"
[5]=>
string(3) "725"
[6]=>
string(1) "X"
[7]=>
string(2) "05"
}
Related
i have some strings result of var_dump like this :
string(5) "tes37"
string(6) "SRV410"
string(6) "SRV400"
string(6) "SRV311"
string(6) "SRV302"
string(6) "SRV301"
string(6) "SRV300"
string(6) "SRV001"
string(7) "SRV_123"
string(5) "sad34"
string(7) "S 0001J"
string(8) "S 00004J"
string(8) "S 00003J"
string(8) "S 00002J"
string(6) "asdasd"
string(4) "4356"
string(3) "234"
here i want to get string with regex such as :
S 0001J, S 00002J, S 00003J and S00004J
anyone can help me out to make the pattern regex ?
Thank you
If you don't care about the number of zeros in the string, you can use the following regex:
S 0+[1-9]J
Change the + to a quantifier of the form {number, number} if you want to keep the number of zeros within a specific number. For example, if you want between three to five zeros, you can do S 0{3,5}[1-9]J.
Of course, this is assuming the string is following the format S (a number of zeros)(a digit)J
I have a string like
"'Joe'&#[Uk Customers.First Name](contact:16[[---]]first_name) +#[Uk Customers.Last Name](contact:16[[---]]last_name)"
My requirement is start finding the pattern
#[A.B](contact:**digit**[[---]]**field**)
There can be many pattern in single string.
and replace it with a new string (entire pattern should be replaced) with a dynamic text generated by digit and field value
For an example for above string there are two matches
1st match is : array(digit => 16, field =>first_name)
2nd match is : array(digit => 16, field =>last_name)
and somewhere I have few rules which are
if digit is 16 and field is first_name replace pattern with "John"
if digit is 16 and field is last_name replace pattern with "Doe"
so the output string will be "'Joe'&John+Doe"
Thanks in advance.
The matching part is fairly straightforward. This will do the trick:
#\[[^.]+\.[^.]+\]\(contact:(\d+)\[\[---\]\]([^)]+)\)
Debuggex Demo
Regex101 Demo
In PHP (and other languages that support named capture groups), you can do this to get the array to contain keys "digit" and "field":
#\[[^.]+\.[^.]+\]\(contact:(?<digit>\d+)\[\[---\]\](?<field>[^)]+)\)
Example PHP code:
$regex = '/#\[[^.]+\.[^.]+\]\(contact:(?<digit>\d+)\[\[---\]\](?<field>[^)]+)\)/';
$text = '"\'Joe\'&#[Uk Customers.First Name](contact:16[[---]]first_name) +#[Uk Customers.Last Name](contact:16[[---]]last_name)"';
preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
var_dump($matches);
Result:
array(2) {
[0]=>
array(5) {
[0]=>
string(55) "#[Uk Customers.First Name](contact:16[[---]]first_name)"
["digit"]=>
string(2) "16"
[1]=>
string(2) "16"
["field"]=>
string(10) "first_name"
[2]=>
string(10) "first_name"
}
[1]=>
array(5) {
[0]=>
string(53) "#[Uk Customers.Last Name](contact:16[[---]]last_name)"
["digit"]=>
string(2) "16"
[1]=>
string(2) "16"
["field"]=>
string(9) "last_name"
[2]=>
string(9) "last_name"
}
}
I'm not real clear on the logic you want to use for the replacement, so I'm afraid I can't help there without some clarification.
I have this string:
a[0]=a[27%a.length];
and this pattern
([a-z])+\[(\S)+\]\=([a-z])+\[+(.*?)+\%+([a-z])+\.length
Preg match array is this one:
array(6) {
[0]=>
string(18) "a[0]=a[27%a.length"
[1]=>
string(1) "a"
[2]=>
string(1) "0"
[3]=>
string(1) "a"
[4]=>
string(0) ""
[5]=>
string(1) "a"
}
Why is the element 4 empty instead of holding the 27?
I think there's one "+" too many. This regex seems to work as you would expect:
([a-z])+\[(\S)+\]\=([a-z])+\[+(.*?)\%+([a-z])+\.length
The following is even cleaner, and it matches the input string:
([a-z])+\[(\S)+\]\=([a-z])+\[(.*?)\%([a-z])+\.length
The difference is that I removed the unnecessary "+" after the squared brackets ("+" means "one or more", while there should be only one bracket in each position).
For an explanation, please refer to regex101, which has a nice formatting for the various parts of the expression: http://regex101.com/r/iB6nH1
Because you used (.*?)+, the matched part will be replaced by empty string at end.
Remove that + then you will get 27 in the match part.
php > preg_match('/([a-z])+\[(\S)+\]\=([a-z])+\[+(.*?)\%+([a-z])+\.length/', $str, $matches);
php > var_dump($matches);
array(6) {
[0]=>
string(18) "a[0]=a[27%a.length"
[1]=>
string(1) "a"
[2]=>
string(1) "0"
[3]=>
string(1) "a"
[4]=>
string(2) "27"
[5]=>
string(1) "a"
}
Consider this code (http://codepad.org/lJGcW7tU):
$str = '1
2
3
4
5
6';
var_dump(explode("\n", $str));
I would expect output like this:
array(6) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
[5]=>
string(1) "6"
}
But actually, it's this:
array(6) {
[0]=>
string(2) "1
"
[1]=>
string(2) "2
"
[2]=>
string(2) "3
"
[3]=>
string(2) "4
"
[4]=>
string(2) "5
"
[5]=>
string(1) "6"
}
The explode seems to have added an extra character to all new array elements, except the last one. This character is not there in the original string, so where did it come from? And why?
A line break is represented differently among different platforms. In Unix-systems, it's simply "\n", while on Windows-based systems it's "\r\n".
Your String probably contains "1\r\n2\r\n3\r\n(...)" which means that when you split it on "\n" the first index of the result is "1\r", the second is "2\r" and so on...
Different possible solutions:
Initialize your string to $str = "1\n2\n3\n4\n5\n6";
Write your PHP file in a text editor that uses Unix-based newlines (some more advanced text editors have a setting for which type of line break to use)
Split on "\r\n" instead of just "\n"
I was just helped in another thread with a regex that has been verified to work. I can see it actually working on Rubular but when I plug the regex into preg_match, I get absolutely nothing.
Here is the regex with my preg_match function:
preg_match('/^!!([0-9]{5}) +.*? +[MF] ([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3}) + ([A-Z])[0-9]{3}([0-9]{4})([0-9]{2})([0-9]{2})/', $res, $matches);
All I am getting is an empty array returned.
The problem is that you have added two extra spaces into the regular expression that should not be there and that cause the match to fail.
/^!!([0-9]{5}) +.*? +[MF] ([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3}) + ([A-Z])...
^ ^
here and here
Whitespace is significant (by default) in regular expressions. A space in a regular expression matches a space in the target string. Removing these two spaces fixes the problem.
See it working on ideone (this time it is a PHP example).
array(10) {
[0]=>
string(39) "!!92519 C 01 M600200BLNBRN D55420090205"
[1]=>
string(5) "92519"
[2]=>
string(3) "600"
[3]=>
string(3) "200"
[4]=>
string(3) "BLN"
[5]=>
string(3) "BRN"
[6]=>
string(1) "D"
[7]=>
string(4) "2009"
[8]=>
string(2) "02"
[9]=>
string(2) "05"
}