Sponsored Content
Top Forums Shell Programming and Scripting Perl RegExp to remove last character from strings Post 302181564 by sysgate on Thursday 3rd of April 2008 10:34:42 AM
Old 04-03-2008
Look at the man for "chop()" function, it does exactly what you want, example :
Code:
#!/usr/bin/perl

my $string = 'frog';
my $chr = chop($string);

print "String: $string\n";
print "Char: $chr\n";

this outputs :
Quote:
String: fro
Char: g
you should, of course, removing this part : "print "Char: $chr\n""

Edit : if you want array :
Quote:
my @array = qw/fred bob jill joan/;
my $chr = chop(@array);

foreach my $str (@array) {
print "$str\n";
}

print "Char: $chr\n";
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

tcl: regexp matching special character

Hello Experts, Can someone help me here: I have a variable which contains a string with "". set var1 {a} set str1 {a is the element i want to match} Now "regexp $var1 $str1" does not work? ("regexp {a\} $str1" works, but var1 gets it's value automatically from another script) Is... (6 Replies)
Discussion started by: sumitgarg
6 Replies

2. Shell Programming and Scripting

Remove parenthesis character (Perl)

Hello, i'm unable to remove the parenthesis character. With $parsed_AsciiName =~ s/\(//; the string is the same And with $parsed_AsciiName =~ s/(//; i retrieve "Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE" Any ideas, please? thank you in advanced. (4 Replies)
Discussion started by: aristegui
4 Replies

3. Shell Programming and Scripting

Perl use split and remove specific character

i want to split the input by a space and remove specific characters like full stop, comma...... etc. and then save each word in an array. i got something below, but it didn't work. can anyone please help me? Thank you #!/usr/bin/perl -w while (<>) { $line = <>; @word = split(' ',... (6 Replies)
Discussion started by: mingming88
6 Replies

4. Shell Programming and Scripting

rename - change n'th character of regexp

Hi, I wonder if its possible to do the following task using rename (perl v5.8.8). I want to find filenames matching the specific pattern and then change chosen character of this pattern to a given character, e.g. do the following renaming: regexp: 'ab' -----> 'a0b' What's the simplest... (0 Replies)
Discussion started by: pms
0 Replies

5. UNIX for Dummies Questions & Answers

Doubt with regexp-meta character

Hi, I am learning reg exp a bit :) Meta char info: {n,m} Matches the preceding character at least n times but not more than m times, for example, 'ba{2,3}b' will find 'baab' and 'baaab' but NOT 'bab' or 'baaaab'. Values are enclosed in braces (curly brackets). Input file: 112 11112... (2 Replies)
Discussion started by: dragon.1431
2 Replies

6. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

7. Shell Programming and Scripting

perl cmd to remove the control-Z character at end of 10GB file

In a 10-50GB file , at end of file there is Control-z character tried the below options, 1. perl -p -i -e 's/^Z//g' new.txt 2. perl -0777lwi -032e0 new.txt and Sed command, dos2unix etc it takes more time to remove the control-z. need a command or perl program to GO TO LAST LINE OF FILE ... (7 Replies)
Discussion started by: prsam
7 Replies

8. Shell Programming and Scripting

Regexp for string that might contain a given character

I'm probably just not thinking of the correct term to search for :-) But I want to match a pattern that might be 'ABC' or '1ABC' there might be three characters, or there might be four, but if there are four, the first has to be 1 (1 Reply)
Discussion started by: jnojr
1 Replies

9. Shell Programming and Scripting

Filter non-alpha character with grep/regexp

Hi all, I am trying to filter out those lines that contain a "non-alpha" character. An example of my input is the following: zygnematales grb zygocactus grb zygocactus_truncatus plt zygodactyl_foot prt zygoma prt zygomatic prt zygomatic_arch prt zygomatic_bone ... (2 Replies)
Discussion started by: owwow14
2 Replies

10. Shell Programming and Scripting

How to remove certain character strings with awk?

Hi all, I need to remove DBPATH= and /db from the string below using awk (or sed, as it also exists on the machine). Input: DBPATH=/some/path/database/db Desired output: /some/path/database Thank you! (8 Replies)
Discussion started by: ejianu
8 Replies
XML::Parser::LiteCopy(3pm)				User Contributed Perl Documentation				XML::Parser::LiteCopy(3pm)

NAME
XML::Parser::LiteCopy - Lightweight regexp-based XML parser SYNOPSIS
use XML::Parser::LiteCopy; $p1 = new XML::Parser::LiteCopy; $p1->setHandlers( Start => sub { shift; print "start: @_ " }, Char => sub { shift; print "char: @_ " }, End => sub { shift; print "end: @_ " }, ); $p1->parse('<foo id="me">Hello World!</foo>'); $p2 = new XML::Parser::LiteCopy Handlers => { Start => sub { shift; print "start: @_ " }, Char => sub { shift; print "char: @_ " }, End => sub { shift; print "end: @_ " }, } ; $p2->parse('<foo id="me">Hello <bar>cruel</bar> World!</foo>'); DESCRIPTION
This Perl implements an XML parser with a interface similar to XML::Parser. Though not all callbacks are supported, you should be able to use it in the same way you use XML::Parser. Due to using experimantal regexp features it'll work only on Perl 5.6 and above and may behave differently on different platforms. Note that you cannot use regular expressions or split in callbacks. This is due to a limitation of perl's regular expression implementation (which is not re-entrant). SUBROUTINES
/METHODS new Constructor. As (almost) all SOAP::Lite constructors, new() returns the object called on when called as object method. This means that the following effectifely is a no-op if $obj is a object: $obj = $obj->new(); New accepts a single named parameter, "Handlers" with a hash ref as value: my $parser = XML::Parser::Lite->new( Handlers => { Start => sub { shift; print "start: @_ " }, Char => sub { shift; print "char: @_ " }, End => sub { shift; print "end: @_ " }, } ); The handlers given will be passed to setHandlers. setHandlers Sets (or resets) the parsing handlers. Accepts a hash with the handler names and handler code references as parameters. Passing "undef" instead of a code reference replaces the handler by a no-op. The following handlers can be set: Init Start Char End Final CData Doctype Comment PI All other handlers are ignored. Calling setHandlers without parameters resets all handlers to no-ops. parse Parses the XML given. In contrast to XML::Parser's parse method, parse() only parses strings. Handler methods Init Called before parsing starts. You should perform any necessary initializations in Init. Start Called at the start of each XML node. See XML::Parser for details. Char Called for each character sequence. May be called multiple times for the characters contained in an XML node (even for every single character). Your implementation has to make sure that it captures all characters. End Called at the end of each XML node. See XML::Parser for details Comment See XML::Parser for details PI See XMLDecl in XML::Parser for details, but also includes other processing instructions Doctype See XML::Parser for details Final Called at the end of the parsing process. You should perform any necessary cleanup here. SEE ALSO
XML::Parser COPYRIGHT
Copyright (C) 2000-2007 Paul Kulchenko. All rights reserved. Copyright (C) 2008 Martin Kutter. All rights reserved. Copyright (C) 2009 Cal Henderson. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This parser is based on "shallow parser" http://www.cs.sfu.ca/~cameron/REX.html Copyright (c) 1998, Robert D. Cameron. AUTHOR
Paul Kulchenko (paulclinger@yahoo.com) Martin Kutter (martin.kutter@fen-net.de) Additional handlers supplied by Adam Leggett. Further modifications by Cal Henderson. perl v5.12.3 2011-06-05 XML::Parser::LiteCopy(3pm)
All times are GMT -4. The time now is 02:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy