I have a lot of strings like:
"1248, 60906068, 4536576, 858687( some text 67, 43, 45)"
And I want to check if the string starts from number and there are brackets in string, in same time I want to get all numbers from the begining to the first bracket. So for this example string result should be like:
[0] => 1248 , [1] => 60906068, [2] => 4536576, [3] => 858687
The point is that in the string after first number at the beginning of the string could be zero additional numbers or one number or even a lot of numbers.
I tried something like that:
^(\d+)(?:,\s?(\d+)?)*\([^\)]+\)$
But it takes only first and last number before brackets.
Is it possible to get all these numbers with only one Regular Expression?
Thank you in advance!
You can use this regex: (\d+)(?:\([^\)]+\))?
All numbers will be captured in Group 1.
See example.
Result:
1248
60906068
4536576
858687
Related
I am trying to extract all words of a .txt file that contants this structure %HOUSE% %CAR%
I am using Preg_match and It´s works but when I have in the same line two words the array return in one position the two words that are in the same line
$rawContent = file($_FILES["file"]["tmp_name"]);
$content = implode(" ",$rawContent);
preg_match_all("/%.*%/",$content,$arrMatches");
Array ( [0] => %HOSTNAME% [1] => %INTERFAZ_LAN% [2] => %IP_LAN% %MASK_LAN% [3] => %ID_INTERFACE_WAN% )
In Position [2] there are two word for example
I think that is a problem of my preg match expression I need to add some
By default, regular expressions using the * character will be "greedy", meaning it will match as many characters as possible. In this case, the expression .* is matching IP_LAN% %MASK_LAN.
To change this bevavior to non-greedy, that is to match as few characters as possible, add a question mark after the asterisk, so your pattern becomes /%.*?%/.
Alternatively, you can change your approach and, rather than match any character any number of times, match anything except the percentage sign any number of times: /%[^%]*%/.
My string:
How would you rate the ease and comfort required to undertake the session?#QUESTION_VALUE_0
How would I be able to get this value specifically from the above string? I don't know what this value will be (apart from that it will be an integer):
(some question)#QUESTION_VALUE_X where X is an integer, I want to get X.
I looked into Regex, but I suck at regular expressions, so I'm at a loss, cheers guys!
About as far as I got with regex
/#QUESTION_VALUE_[0-9]+/
But I can't get the number out of the string. How can I only grab the number?
This should work for you:
Just put the escape sequence \d (which means 0-9) with the quantifier + (which means 1 or more times) into a group (()) to capture the the number which you then can access in the array $m.
<?php
$str = "How would you rate the ease and comfort required to undertake the session?#QUESTION_VALUE_0";
preg_match("/#QUESTION_VALUE_(\d+)/", $str, $m);
echo $m[1];
?>
output:
0
If you do print_r($m); you will see the structure of your array:
Array
(
[0] => #QUESTION_VALUE_0
[1] => 0
)
And now you see ^ that you have the full match in the first element and then first group ((\d+)) in the second element.
This regex finds the right string, but only returns the first result. How do I make it search the rest of the text?
$text =",415.2109,520.33970,495.274100,482.3238,741.5634
655.3444,488.29980,741.5634";
preg_match("/[^,]+[\d+][.?][\d+]*/",$text,$data);
echo $data;
Follow up:
I'm pushing the initial expectations of this script, and I'm at the point where I'm pulling out more verbose data. Wasted many hours with this...can anyone shed some light?
heres my string:
155.101.153.123:simple:mass_mid:[479.0807,99.011, 100.876],mass_tol:[30],mass_mode: [1],adducts:[M+CH3OH+H],
130.216.138.250:simple:mass_mid:[290.13465,222.34566],mass_tol:[30],mass_mode:[1],adducts:[M+Na],
and heres my regex:
"/mass_mid:[((?:\d+)(?:.)(?:\d+)(?:,)*)/"
I'm really banging my head on this one! Can someone tell me how to exclude the line mass_mid:[ from the results, and keep the comma seperated values?
Use preg_match_all rather than preg_match
From the PHP Manual:
(`preg_match_all`) searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.
After the first match is found, the subsequent searches are continued on from end of the last match.
http://php.net/manual/en/function.preg-match-all.php
Don't use a regex. Use split to split apart your inputs on the commas.
Regexes are not a magic wand you wave at every problem that happens to involve strings.
Description
To extract a list of numeric values which may include a single decimal point, then you could use this regex
\d*\.?\d+
PHP Code Example:
<?php
$sourcestring=",415.2109,520.33970,495.274100,482.3238,741.5634
655.3444,488.29980,741.5634";
preg_match_all('/\d*\.?\d+/im',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
yields matches
$matches Array:
(
[0] => Array
(
[0] => 415.2109
[1] => 520.33970
[2] => 495.274100
[3] => 482.3238
[4] => 741.5634
[5] => 655.3444
[6] => 488.29980
[7] => 741.5634
)
)
Let's say I want to extract a list of sections from an email that are listed in the format
Section 26, 753, 87, 201, 47
I know that this certain kind of formatting is present in my document but I have no idea where. How can I write a regex that will extract all of the section numbers? (Sorry, I'll post the pattern I already have later.) Currently, it looks for the section phrase, followed by a space, followed by a number. How are the rest extracted? Perhaps 0 or more repetitions of comma, space, number? How exactly is that formatted?
Directly returning a variable number of captures from a regex is not possible with PHP/PCRE (although there are implementations that support this, notably .NET and Perl 6).
With PHP, you have to write code. There are a variety of options - remove matches from the string in a loop, extract the list and then use preg_match_all to get the numbers, and so on - but I think I would just extract the whole list into its own string and use split (well, preg_split) to get the individual section numbers:
$str = 'Section 26, 753, 87, 201, 47';
if (preg_match('/Section\s+(\d+(?:,\s*\d+)*)/', $str, $match)) {
$sections = preg_split('/,\s*/', $match[1]);
}
print_r($sections);
Which gives the desired result:
Array (
[0] => 26
[1] => 753
[2] => 87
[3] => 201
[4] => 47
)
i have this string ++++++1DESIGNRESULTSM25Fe415(Main)
and i have similar string about 2000 lines from which i want to split these..
[++++++] [1] [DESIGNRESULTS] [M25] [Fe415] [(Main)]
from the pattern only the 2nd 4h and 5th value changes
eg.. ++++++2DESIGNRESULTSM30Fe418(Main) etc..
what i actually want is:
Split the first value [++++++]
Split the value after 4 Character of [DESIGNRESULTS] so ill get this [M25]
Split the value before 4 Character of [(Main)] so ill get this [Fe415]
After all this done store the final chunk of piece in an array.
the similar output what i want is
Array ( [0] => 1 [1] => M25 [2] => Fe415 )
Please help me with this...
Thanks in advance :)
Your data split needs are a bit unclear. A regular expression that will get separate matches on each of the chunks you first specify:
(\++)(\d)(DESIGNRESULTS)(M\d\d)(Fe\d\d\d)(\(Main\))
If you only need the two you are asking for at the end, you can use
(\d)DESIGNRESULTS(M\d\d)(Fe\d\d\d)
You could also replace \d\d with \d+ if the number of digits is unknown.
However, based on your examples it looks like each string chunk is a consistent length. It would be even faster to use
array(
substr($string, 6, 1)
//...
)
How about this
$str = "++++++1DESIGNRESULTSM25Fe415(Main)";
$match = array();
preg_match("/^\+{0,}(\d)DESIGNRESULTS(\w{3})(\w{5})/",$str,$match);
array_shift($match);
print_r($match);