Sponsored Content
Top Forums Programming How to trim the white space around a string in C program Post 302245688 by monikersupreme on Friday 10th of October 2008 05:10:39 PM
Old 10-10-2008
Quote:
Originally Posted by redoubtable
You could also use POSIX's regular expressions (regcomp(), regexec(), regfree()).

First you compile a regex using regcomp(), then you can match it several times to a certain string using regexec(). Finally you should free the compiled regex with regfree().

A very simple example:
Code:
#include <stdio.h>
#include <regex.h>

int
main ()
{
    regex_t reg;
    char * aaa = "          ";
    
    if (regcomp (&reg, "[^[:space:]$]", REG_EXTENDED|REG_NOSUB) != 0)
        exit(1);
    if ((regexec (&reg, aaa, 0, (regmatch_t *) NULL, 0)) == 0)
         printf ("no match\n");
    else
        printf ("match\n");
    regfree(&reg);
}

This code doesn't compile (regcomp throws an error)... is there any way to get regcomp/regexec to recognize whitespace (using "_" doesn't work) or a word boundary?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to trim space in output variable ?

Hi , I have a code like this: uid=scott password=tiger database=db01 cat >runid_val.sql<<-EOA SET ECHO OFF SET FEEDBACK OFF SET HEADING OFF SELECT trim(runid_seq.nextval) FROM dual; EXIT EOA echo `cat runid_val.sql` V_RUNID=`sqlplus -s $uid/$password@$database @runid_val.sql`... (5 Replies)
Discussion started by: vj_76
5 Replies

2. Shell Programming and Scripting

Trim white spaces using awk

Hi, I have a CSV file with footer information as below. The third value is the number of records in the file. Sometimes it contains both leading and trailing white spaces which i want to trim using awk. C,FOOTER , 00000642 C,FOOTER , 00000707 C, FOOTER,... (2 Replies)
Discussion started by: mona
2 Replies

3. Shell Programming and Scripting

stripping white space...

Hi All; Having a problem with a file.. the file contains the following data... (a snapshot) 1331F9E9DB7C2BB80EAEDE3A8F043B94,AL7 1DZ,M,50 186FDF93E1303DBA217279EC3671EA91,NG5 1JU,M,24 3783FFAF602015056A8CD21104B1AAAF,CH42 4NQ,M,17 It has 3 columns sepreated by a , the second column... (7 Replies)
Discussion started by: Zak
7 Replies

4. UNIX for Dummies Questions & Answers

SED with White Space

Dear Members, Suppose i have a variable test which stores a string as below: test='John drives+++++++++a+++++car' now i want to use sed on the above variable and replace + with a white space, so that i get echo $test should give me 'john drives a car' Between... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

5. Shell Programming and Scripting

sed + white space

Hi, What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters. My idea (without testing) sed 's/ //g' Is there a better way? (18 Replies)
Discussion started by: mcclunyboy
18 Replies

6. UNIX for Dummies Questions & Answers

filename with white space

our user creates a text file with a white space on the filename. this same file is transfered to unix via automation tool. i have a korn shell script that reads these files on a input directory and connects to oracle database to run the oracle procedures which will load the data from each of the... (2 Replies)
Discussion started by: wtolentino
2 Replies

7. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

8. Shell Programming and Scripting

Add white space

hi guys how can i add spacein file name with sed if strings have no space around dash input 19-20 ( 18-19 ) ABC-EFG output after add white space 19 - 20 (18 - 19 ) ABC - EFG thx in advance (2 Replies)
Discussion started by: mhs
2 Replies

9. Shell Programming and Scripting

Putting white Space at the end of the string

Hi Guys, Hope, you all are doing good out there. I am writing a shell script and currrint in need of your help. This is what I need to do; I have position based plain file. One of the fields is 15 character long. I need to fill that field. The problem is that the value is dynamic, it could... (4 Replies)
Discussion started by: singh.chandan18
4 Replies

10. Shell Programming and Scripting

Trim Space

In Shell, I have output of a unix command as test1 test2015 but I want it as test1 test2015 can anyone help me out. Use code tags, thanks. (3 Replies)
Discussion started by: OscarS
3 Replies
REGEX(3)						     Linux Programmer's Manual							  REGEX(3)

NAME
regcomp, regexec, regerror, regfree - POSIX regex functions SYNOPSIS
#include <sys/types.h> #include <regex.h> int regcomp(regex_t *preg, const char *regex, int cflags); int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags); size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size); void regfree(regex_t *preg); DESCRIPTION
POSIX Regex Compiling regcomp() is used to compile a regular expression into a form that is suitable for subsequent regexec() searches. regcomp() is supplied with preg, a pointer to a pattern buffer storage area; regex, a pointer to the null-terminated string and cflags, flags used to determine the type of compilation. All regular expression searching must be done via a compiled pattern buffer, thus regexec() must always be supplied with the address of a regcomp() initialized pattern buffer. cflags may be the bitwise-or of one or more of the following: REG_EXTENDED Use POSIX Extended Regular Expression syntax when interpreting regex. If not set, POSIX Basic Regular Expression syntax is used. REG_ICASE Do not differentiate case. Subsequent regexec() searches using this pattern buffer will be case insensitive. REG_NOSUB Support for substring addressing of matches is not required. The nmatch and pmatch arguments to regexec() are ignored if the pat- tern buffer supplied was compiled with this flag set. REG_NEWLINE Match-any-character operators don't match a newline. A nonmatching list ([^...]) not containing a newline does not match a newline. Match-beginning-of-line operator (^) matches the empty string immediately after a newline, regardless of whether eflags, the execu- tion flags of regexec(), contains REG_NOTBOL. Match-end-of-line operator ($) matches the empty string immediately before a newline, regardless of whether eflags contains REG_NOTEOL. POSIX Regex Matching regexec() is used to match a null-terminated string against the precompiled pattern buffer, preg. nmatch and pmatch are used to provide information regarding the location of any matches. eflags may be the bitwise-or of one or both of REG_NOTBOL and REG_NOTEOL which cause changes in matching behavior described below. REG_NOTBOL The match-beginning-of-line operator always fails to match (but see the compilation flag REG_NEWLINE above) This flag may be used when different portions of a string are passed to regexec() and the beginning of the string should not be interpreted as the begin- ning of the line. REG_NOTEOL The match-end-of-line operator always fails to match (but see the compilation flag REG_NEWLINE above) Byte Offsets Unless REG_NOSUB was set for the compilation of the pattern buffer, it is possible to obtain substring match addressing information. pmatch must be dimensioned to have at least nmatch elements. These are filled in by regexec() with substring match addresses. Any unused structure elements will contain the value -1. The regmatch_t structure which is the type of pmatch is defined in <regex.h>. typedef struct { regoff_t rm_so; regoff_t rm_eo; } regmatch_t; Each rm_so element that is not -1 indicates the start offset of the next largest substring match within the string. The relative rm_eo element indicates the end offset of the match, which is the offset of the first character after the matching text. Posix Error Reporting regerror() is used to turn the error codes that can be returned by both regcomp() and regexec() into error message strings. regerror() is passed the error code, errcode, the pattern buffer, preg, a pointer to a character string buffer, errbuf, and the size of the string buffer, errbuf_size. It returns the size of the errbuf required to contain the null-terminated error message string. If both errbuf and errbuf_size are nonzero, errbuf is filled in with the first errbuf_size - 1 characters of the error message and a terminating null. POSIX Pattern Buffer Freeing Supplying regfree() with a precompiled pattern buffer, preg will free the memory allocated to the pattern buffer by the compiling process, regcomp(). RETURN VALUE
regcomp() returns zero for a successful compilation or an error code for failure. regexec() returns zero for a successful match or REG_NOMATCH for failure. ERRORS
The following errors can be returned by regcomp(): REG_BADBR Invalid use of back reference operator. REG_BADPAT Invalid use of pattern operators such as group or list. REG_BADRPT Invalid use of repetition operators such as using '*' as the first character. REG_EBRACE Un-matched brace interval operators. REG_EBRACK Un-matched bracket list operators. REG_ECOLLATE Invalid collating element. REG_ECTYPE Unknown character class name. REG_EEND Non specific error. This is not defined by POSIX.2. REG_EESCAPE Trailing backslash. REG_EPAREN Un-matched parenthesis group operators. REG_ERANGE Invalid use of the range operator, e.g., the ending point of the range occurs prior to the starting point. REG_ESIZE Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2. REG_ESPACE The regex routines ran out of memory. REG_ESUBREG Invalid back reference to a subexpression. CONFORMING TO
POSIX.1-2001. SEE ALSO
grep(1), regex(7) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2008-05-29 REGEX(3)
All times are GMT -4. The time now is 04:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy