$regex = "/(.+),(.+);/";
$input = "somestring, 234, sometring5";
preg_match_all($regex, $input, $matches, PREG_SET_ORDER);
I've tried to make it like this:
$regex = "/(.^,+),(.^,+);/";
$input = "somestring, 234, sometring5";
preg_match_all($regex, $input, $matches, PREG_SET_ORDER);
But it doesn't work, because I thought that ^, means except commas, but why it doesn't work?
Because I want to group them by commas, but the commas are symbols itself that the parser gets how to avoid this?
You could just split the string on , and trim the result:
$matches = array_map('trim', explode(',', $input));
Here's a modified version of your RegEx, with some explanations for each modifier.
And for those not willing to visit the link:
A RegEx to match all words in a sentence is /([a-zA-Z0-9]*)/g
[a-zA-Z0-9] means match all non symbol characters (a-z, A-Z and 0-9)
* means match it as many repeating times as possible
g modifier (the /g at the end) means match as many as possible inside the string; don't just stop at the first one.
$regex = "/([^,])/g";
$input = "somestring, 234, sometring5";
preg_match_all($regex, $input, $matches, PREG_SET_ORDER);
will get you everything except commas.
Related
I am trying to detect a string inside the following pattern: [url('example')] in order to replace the value.
I thought of using a regex to get the strings inside the squared brackets and then another to get the text inside the parenthesis but I am not sure if that's the best way to do it.
//detect all strings inside brackets
preg_match_all("/\[([^\]]*)\]/", $text, $matches);
//loop though results to get the string inside the parenthesis
preg_match('#\((.*?)\)#', $match, $matches);
To match the string between the parenthesis, you might use a single pattern to get a match only:
\[url\(\K[^()]+(?=\)])
The pattern matches:
\[url\( Match [url(
\K Clear the current match buffer
[^()]+ Match 1+ chars other than ( and )
(?=\)]) Positive lookahead, assert )] to the right
See a regex demo.
For example
$re = "/\[url\(\K[^()]+(?=\)])/";
$text = "[url('example')]";
if (preg_match($re, $text, $match)) {
var_dump($match[0]);;
}
Output
string(9) "'example'"
Another option could be using a capture group. You can place the ' inside or outside the group to capture the value:
\[url\(([^()]+)\)]
See another regex demo.
For example
$re = "/\[url\(([^()]+)\)]/";
$text = "[url('example')]";
if (preg_match($re, $text, $match)) {
var_dump($match[1]);;
}
Output
string(9) "'example'"
I need to extract the highlighted numbers from each line in a CSV file.
Currently I am looping though the lines & splitting the line on the / character as this only appears once in each row, but how do I remove everything around these numbers so I am left with:
9/10
10/11
11/12
...
If you want to only get the numbers, you could do a preg_match
$re = '/(\d+\/\d+)/s';
$str = 'dfsadsfadsfads~~9/10~~lfkjdskfds';
preg_match($re, $str, $matches);
but if you are able to get the entire doc as a single string, you could do a preg_match_all
$re = '/(\d+\/\d+)/s';
$str = 'dfsadsfadsfads~~9/10~~lfkjdskfds\ndfsadsfadsfads~~9/10~~lfkjdskfds\n';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
then loop on the $matches
I want to convert certain patterns into links and it works fine as far as normal user ids are considered.But now i want to do the same for encrypted ids as well.
Below is my code:(works)
$text = "hi how are you guys???... ##[Sam Thomas:10181] ##[Jack Daniel:11074] ##[Paul Walker:11043] ";
$pattern = "/##\[([^:]*):(\d*)\]/";
$matches = array();
preg_match_all($pattern, $text, $matches);
$output = preg_replace($pattern, "$1", $text);
Now i need to do link the text like:
"hi how are you guys???... ##[Sam Thomas:ZGNjAmD9ac3K] ##[Jack Daniel:ZGNjAmD9ac3K] ##[Paul Walker:ZGNjAmD9ac3K] ";
But this encrypted is not identified by above regular expression...
##\[([^:]*):(.*?)\]
^^
Try this.See demo.Just change \d* to .*? to accept anything or \w* to accept only numbers and letters.or [^\]]* or [0-9a-zA-Z] as well.
https://regex101.com/r/vD5iH9/52
Change your regex to accept numbers and letters as well.
Something like this -
##\[([^:]*):([0-9a-zA-Z]*)\]
^^^^^^^^^^^ Replaced \d
Demo
I'm trying to use PHP regular expressions. I've tried this code:
$regex = "c:(.+),";
$input = "otherStuff094322f98c:THIS,OtherStuffHeree129j12dls";
$match = Array();
preg_match_all($regex, $input, $match);
It should return a sub-string THIS ("c" and ":" followed by any character combination followed by ",") from $input. But it returns a empty array. What am I doing wrong?
I think you need the slashes to make regex working.
and using .+ will match everything behind the comma too, which is you don't want. Use .+? or [^,]+
$regex = "/c:(.+?),/";
or
$regex = "/c:([^,]+),/";
I use a simple preg_match_all to find the occurrence of a list of words in a text.
$pattern = '/(word1|word2|word3)/';
$num_found = preg_match_all( $pattern, $string, $matches );
But this also match subset of words like abcword123. I need it to find word1, word2 and word3 when they're occurring as full words only. Note that this doesn't always mean that they're separated by spaces on both sides, it could be a comma, semi-colon, period, exclamation mark, question mark, or another punctuation.
IF you are looking to match "word1", "word2", "word3" etc only then using in_array is always better. Regex are super powerful but it takes a lot of cpu power also. So try to avoid it when ever possible
$words = array ("word1", "word2", "word3" );
$found = in_array ($string, $words);
check PHP: in_array - Manual for more information on in_array
And if you want to use regex only try
$pattern = '/^(word1|word2|word3)$/';
$num_found = preg_match_all( $pattern, $string, $matches );
And if you want to get something like "this statement has word1 in it", then use "\b" like
$pattern = '/\b(word1|word2|word3)\b/';
$num_found = preg_match_all( $pattern, $string, $matches );
More of it here PHP: Escape sequences - Manual search for \b
Try:
$pattern = '/\b(word1|word2|word3)\b/';
$num_found = preg_match_all( $pattern, $string, $matches );
You can use \b to match word boundaries. So you want to use /\b(word1|word2|word3)\b/ as your regex.