Hi,
Can anyone help me to find regular expression for the following in Perl?
"The string can only contain lower case letters (a-z) and no more than one of any letter."
For example: "table" is accepted, whether "dude" is not.
I have coded like this:
$str = "table";
if ($str =~ m/\b()\b/) {... (4 Replies)
Hi
I need to do this thing in awk (or perl?). I try to find out how can I identify 1st and 2nd result from the OR expression in gensub:
block='title Sata Mandriva
kernel /boot/vmlinuz
initrd /boot/initrd.img'
echo "$block" | awk '{ x=gensub(/(kernel|initrd) /,"\\1XXX","g"); print x }'
... (12 Replies)
I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly:
if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Hi,
I get the following when I cat a file *.log
xxxxx
=====
dasdas gwdgsg fdsagfsag agsdfag
=====
random data
=====
My output should look like :
If the random data after the 2nd ==== is null then OK should be printed else
the random data should be printed.
How do I go about this... (5 Replies)
Hi Guys
I have the following regex
$OSRELEASE = $1 if ($output =~ /(Mac OS X (Server )?10.\d)/);
output is currently
Mac OS X 10.7.5
when the introduction of Mac 10.8 output changes to
OS X 10.8.2
they have dropped the Mac bit so i changed the regex to be (2 Replies)
Hello,
I'm trying to get a quick help on regex since i'm not a regular programmer.
Below is the line i'm trying to apply my regex to..i want to use the regex in a for loop and this line will keep on changing.
subject=... (4 Replies)
Could anyone please make me understand how the ?= works below ..
After executing this I am getting the same output.
$string="I love chocolate.";
$string =~ s/chocolate(?= ice)/vanilla/;
print "$string\n"; (2 Replies)
I am not a big expert in regex and have just little understanding of that language.
Could you help me to understand the regular Perl expression:
^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{
------
This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Experts -
I found a script on one of the servers that I work on and I need help understanding
one of the lines.
I know what the script does, but I'm having a hard time understanding the grouping.
Can someone help me with this?
Here's the script...
#!/usr/bin/perl
use strict;
use... (2 Replies)
Discussion started by: timj123
2 Replies
LEARN ABOUT PHP
preg_split
PREG_SPLIT(3) 1 PREG_SPLIT(3)preg_split - Split string by a regular expressionSYNOPSIS
array preg_split (string $pattern, string $subject, [int $limit = -1], [int $flags])
DESCRIPTION
Split the given string by a regular expression.
PARAMETERS
o $pattern
- The pattern to search for, as a string.
o $subject
- The input string.
o $limit
- If specified, then only substrings up to $limit are returned with the rest of the string being placed in the last substring. A
$limit of -1, 0 or NULL means "no limit" and, as is standard across PHP, you can use NULL to skip to the $flags parameter.
o $flags
-$flags can be any combination of the following flags (combined with the | bitwise operator):
o PREG_SPLIT_NO_EMPTY - If this flag is set, only non-empty pieces will be returned by preg_split(3).
o PREG_SPLIT_DELIM_CAPTURE - If this flag is set, parenthesized expression in the delimiter pattern will be captured and
returned as well.
o PREG_SPLIT_OFFSET_CAPTURE - If this flag is set, for every occurring match the appendant string offset will also be
returned. Note that this changes the return value in an array where every element is an array consisting of the matched
string at offset 0 and its string offset into $subject at offset 1.
RETURN VALUES
Returns an array containing substrings of $subject split along boundaries matched by $pattern.
EXAMPLES
Example #1
preg_split(3) example : Get the parts of a search string
<?php
// split the phrase by any number of commas or space characters,
// which include " ",
, ,
and f
$keywords = preg_split("/[s,]+/", "hypertext language, programming");
print_r($keywords);
?>
The above example will output:
Array
(
[0] => hypertext
[1] => language
[2] => programming
)
Example #2
Splitting a string into component characters
<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>
The above example will output:
Array
(
[0] => s
[1] => t
[2] => r
[3] => i
[4] => n
[5] => g
)
Example #3
Splitting a string into matches and their offsets
<?php
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?>
The above example will output:
Array
(
[0] => Array
(
[0] => hypertext
[1] => 0
)
[1] => Array
(
[0] => language
[1] => 10
)
[2] => Array
(
[0] => programming
[1] => 19
)
)
NOTES
Tip
If you don't need the power of regular expressions, you can choose faster (albeit simpler) alternatives like explode(3) or
str_split(3).
Tip
If matching fails, an array with a single element containing the input string will be returned.
SEE ALSO
PCRE Patterns, preg_quote(3), implode(3), preg_match(3), preg_match_all(3), preg_replace(3), preg_last_error(3).
PHP Documentation Group PREG_SPLIT(3)