Sponsored Content
Top Forums Shell Programming and Scripting Regular Expression - Switch Entire String Post 302923959 by rjulich on Wednesday 5th of November 2014 01:32:51 PM
Old 11-05-2014
Scissors Regular Expression - Switch Entire String

I would like to be able to use a regular expression to find and replace entire strings, but not replace if the string is a substring in a larger string.

Example:
Code:
$string = "ABC ABCDEF ABC ABCDEF ABC";
Something like - $string =~ s/ABC/XYZ/g;

->Desired:
Code:
$string = "XYZ ABCDEF XYZ ABCDEF XYZ";

->NOT:
Code:
$string = "XYZ XYZDEF XYZ XYZDEF XYZ";


Last edited by Don Cragun; 11-05-2014 at 02:40 PM.. Reason: Add CODE tags.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Regular Expression - match 'b' that follows 'a' and is at the end of a string

Hi, I'm struggling with a regex that would match a 'b' that follows an 'a' and is at the end of a string of non-white characters. For example: Line 1: aba abab b abb aab bab baa I can find the right strings but I'm lacking knowledge of how to "discard" the bits that precede bs.... (2 Replies)
Discussion started by: machinogodzilla
2 Replies

2. Shell Programming and Scripting

validate a string against a regular expression

Hi there i have a script which will create unix user accounts. Id like to validate the entered string so that it is specifically 8 characters or less and consists of only ! not Is there a way to validate a string against a regular expression.. i.e size=`printf "$var | wc -m` ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

3. Shell Programming and Scripting

Help: Regular Expression for Negate Matching String

Hi guys, as per subject I am having problem with regular expressions. Example, if i got a string "javax.servlet.http.HttpServlet.service" that may occurred anywhere within a text file. How can I used the negate pattern matching of regular expression? I tried the below pattern but it... (4 Replies)
Discussion started by: DrivesMeCrazy
4 Replies

4. Shell Programming and Scripting

regular expression format string in one line.

Hi All, @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); $day=091023; $day_combine = $day; $day_combine =~ s/({2})({2})({2})/20$1-$months-$3/; Instead of three lines, is possible to combine the last two lines into a single line? means no need assign $day to $day_combine... (2 Replies)
Discussion started by: jimmy_y
2 Replies

5. Shell Programming and Scripting

Regular Expression doesn't match dot "." in a string

hello, I am writting a regular expression that intend to match any tunnel or serial interface but it doesn't mtach any serial sub-interface. For example, statement should match "Tunnel3" or "Serial0/1" but shouldn't match "Serial0\1.1" (doesn't include dot ".") I tried the following but... (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

6. Programming

Perl regular expression to check string ending

Hi, I am trying to write a regular expression in perl to check if the string end's with "numbers-numbers" or "-numbers". I experimented something like m/\d*-\d*$/ , but this is not solving my problem. Can anyone help me in writing this expression? Well spelled titles and proper use of code... (2 Replies)
Discussion started by: successlin
2 Replies

7. Shell Programming and Scripting

How can awk search a string without using regular expression?

Hello, Awk seem treat the pattern as regular expression, how can awk search not using regular expression? e.g. just represent for "", not "A" or "a" . I don't want to add backslash . (2 Replies)
Discussion started by: 915086731
2 Replies

8. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

9. Shell Programming and Scripting

String regular expression

Hi, temp="/usr=25,/usr/lib=12" How to get only dir names with out values. I tried like below but no use. tmp=${temp##*,} echo $tmp o/p: /usr/lib=12 expected o/p: /usr /usr/lib ---> in array (13 Replies)
Discussion started by: munna_dude
13 Replies

10. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies
regexp(3erl)						     Erlang Module Definition						      regexp(3erl)

NAME
regexp - Regular Expression Functions for Strings DESCRIPTION
Note: This module has been obsoleted by the re module and will be removed in a future release. This module contains functions for regular expression matching and substitution. EXPORTS
match(String, RegExp) -> MatchRes Types String = RegExp = string() MatchRes = {match,Start,Length} | nomatch | {error,errordesc()} Start = Length = integer() Finds the first, longest match of the regular expression RegExp in String . This function searches for the longest possible match and returns the first one found if there are several expressions of the same length. It returns as follows: {match,Start,Length} : if the match succeeded. Start is the starting position of the match, and Length is the length of the matching string. nomatch : if there were no matching characters. {error,Error} : if there was an error in RegExp . first_match(String, RegExp) -> MatchRes Types String = RegExp = string() MatchRes = {match,Start,Length} | nomatch | {error,errordesc()} Start = Length = integer() Finds the first match of the regular expression RegExp in String . This call is usually faster than match and it is also a useful way to ascertain that a match exists. It returns as follows: {match,Start,Length} : if the match succeeded. Start is the starting position of the match and Length is the length of the matching string. nomatch : if there were no matching characters. {error,Error} : if there was an error in RegExp . matches(String, RegExp) -> MatchRes Types String = RegExp = string() MatchRes = {match, Matches} | {error, errordesc()} Matches = list() Finds all non-overlapping matches of the expression RegExp in String . It returns as follows: {match, Matches} : if the regular expression was correct. The list will be empty if there was no match. Each element in the list looks like {Start, Length} , where Start is the starting position of the match, and Length is the length of the matching string. {error,Error} : if there was an error in RegExp . sub(String, RegExp, New) -> SubRes Types String = RegExp = New = string() SubRes = {ok,NewString,RepCount} | {error,errordesc()} RepCount = integer() Substitutes the first occurrence of a substring matching RegExp in String with the string New . A & in the string New is replaced by the matched substring of String . & puts a literal & into the replacement string. It returns as follows: {ok,NewString,RepCount} : if RegExp is correct. RepCount is the number of replacements which have been made (this will be either 0 or 1). {error, Error} : if there is an error in RegExp . gsub(String, RegExp, New) -> SubRes Types String = RegExp = New = string() SubRes = {ok,NewString,RepCount} | {error,errordesc()} RepCount = integer() The same as sub , except that all non-overlapping occurrences of a substring matching RegExp in String are replaced by the string New . It returns: {ok,NewString,RepCount} : if RegExp is correct. RepCount is the number of replacements which have been made. {error, Error} : if there is an error in RegExp . split(String, RegExp) -> SplitRes Types String = RegExp = string() SubRes = {ok,FieldList} | {error,errordesc()} Fieldlist = [string()] String is split into fields (sub-strings) by the regular expression RegExp . If the separator expression is " " (a single space), then the fields are separated by blanks and/or tabs and leading and trailing blanks and tabs are discarded. For all other values of the separator, leading and trailing blanks and tabs are not discarded. It returns: {ok, FieldList} : to indicate that the string has been split up into the fields of FieldList . {error, Error} : if there is an error in RegExp . sh_to_awk(ShRegExp) -> AwkRegExp Types ShRegExp AwkRegExp = string() SubRes = {ok,NewString,RepCount} | {error,errordesc()} RepCount = integer() Converts the sh type regular expression ShRegExp into a full AWK regular expression. Returns the converted regular expression string. sh expressions are used in the shell for matching file names and have the following special characters: * : matches any string including the null string. ? : matches any single character. [...] : matches any of the enclosed characters. Character ranges are specified by a pair of characters separated by a - . If the first character after [ is a ! , then any character not enclosed is matched. It may sometimes be more practical to use sh type expansions as they are simpler and easier to use, even though they are not as pow- erful. parse(RegExp) -> ParseRes Types RegExp = string() ParseRes = {ok,RE} | {error,errordesc()} Parses the regular expression RegExp and builds the internal representation used in the other regular expression functions. Such representations can be used in all of the other functions instead of a regular expression string. This is more efficient when the same regular expression is used in many strings. It returns: {ok, RE} if RegExp is correct and RE is the internal representation. : {error, Error} if there is an error in RegExpString . : format_error(ErrorDescriptor) -> Chars Types ErrorDescriptor = errordesc() Chars = [char() | Chars] Returns a string which describes the error ErrorDescriptor returned when there is an error in a regular expression. REGULAR EXPRESSIONS
The regular expressions allowed here is a subset of the set found in egrep and in the AWK programming language, as defined in the book, The AWK Programming Language, by A. V. Aho, B. W. Kernighan, P. J. Weinberger . They are composed of the following characters: c : matches the non-metacharacter c . c : matches the escape sequence or literal character c . . : matches any character. ^ : matches the beginning of a string. $ : matches the end of a string. [abc...] : character class, which matches any of the characters abc... Character ranges are specified by a pair of characters separated by a - . [^abc...] : negated character class, which matches any character except abc... . r1 | r2 : alternation. It matches either r1 or r2 . r1r2 : concatenation. It matches r1 and then r2 . r+ : matches one or more r s. r* : matches zero or more r s. r? : matches zero or one r s. (r) : grouping. It matches r . The escape sequences allowed are the same as for Erlang strings:  : backspace f : form feed : newline (line feed) : carriage return : tab e : escape v : vertical tab s : space d : delete ddd : the octal value ddd xhh : The hexadecimal value hh . x{h...} : The hexadecimal value h... . c : any other character literally, for example \ for backslash, " for ") To make these functions easier to use, in combination with the function io:get_line which terminates the input line with a new line, the $ characters also matches a string ending with "... " . The following examples define Erlang data types: Atoms [a-z][0-9a-zA-Z_]* Variables [A-Z_][0-9a-zA-Z_]* Floats (+|-)?[0-9]+.[0-9]+((E|e)(+|-)?[0-9]+)? Regular expressions are written as Erlang strings when used with the functions in this module. This means that any or " characters in a regular expression string must be written with as they are also escape characters for the string. For example, the regular expression string for Erlang floats is: "(\+|-)?[0-9]+\.[0-9]+((E|e)(\+|-)?[0-9]+)?" . It is not really necessary to have the escape sequences as part of the regular expression syntax as they can always be generated directly in the string. They are included for completeness and can they can also be useful when generating regular expressions, or when they are entered other than with Erlang strings. Ericsson AB stdlib 1.17.3 regexp(3erl)
All times are GMT -4. The time now is 05:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy