Doubt on RegEx

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doubt on RegEx
# 1  
Old 06-18-2011
Doubt on RegEx


I have a silly doubt:

Assume that I have a line like this

Heading: Value1; SomeText1 (a, b, c), Value 2; SomeText2 (d, e, f)

I wanted to remove all semicolon and remove everything in brackets (including brackets). I managed to do this using this code

PHP Code:
if (strstr($line,'Heading')){
$new_heading str_replace(";"""$line); // Replaces semi-colon
$new_heading preg_replace("/\([^\)]+\)/","",$new_heading); //Removes Text With in Brackets
$line $new_heading;
$line//Outputs "Heading: Value1 SomeText1 , Value 2 SomeText2"

Now Assume I have a line like this

Heading: Text1 (a, b) Text2. (d, f) Text3 (g, h)

What I want to achieve is... Remove everything with in brackets (inclusive brackets) and replace it with comma. However the last ocurance of the bracket should not be replace with a a comma.

I mean the output should be

Heading: Text1 , Text2. , Text3

How to achive this?
# 2  
Old 06-19-2011

$heading = "Heading: Text1 (a, b) Text2. (d, f) Text3 (g, h)";
$new_heading = rtrim(preg_replace("/\([^\)]+\)/",",",$heading),',');
echo $new_heading;


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

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)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting


Hi experts, In one of our code we have used some command like this. name=${name##*/} Can someone please let me know what exactly it does? Thanks & Regards, Sathya V. (1 Reply)
Discussion started by: Sathya83aa
1 Replies

4. Shell Programming and Scripting

Doubt with regex to replace square bracket

there is a word "welcome" output should be "welcome\ i am using regsub to add backslash "\" in place where ever i find square brackets (open or close).. But i am not getting it... pls help out.. set a {welcome} set d (5 Replies)
Discussion started by: Syed Imran
5 Replies

5. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Red Hat


I need help in opening the .exe files in linux. As i have downloaded ubuntu os from trail version. after executing the the file name in terminal it is stating that get archive files for opening the .exe files. But i am unable to get it please help me (2 Replies)
Discussion started by: yashwanthguru
2 Replies

7. UNIX for Dummies Questions & Answers


Hi , Struck with one basic question. Iam expecting word count of 4 where "wc" is showing as 5 . # echo "abcd" | wc 1 1 5 # echo abcd | wc 1 1 5 (5 Replies)
Discussion started by: penchal_boddu
5 Replies

8. Shell Programming and Scripting

Converting perl regex to sed regex

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)
Discussion started by: suntzu
1 Replies

9. UNIX for Dummies Questions & Answers


How would i create virtual interface in linux to configure more than one IP address for a physical interface? any help wll be appreciated. (1 Reply)
Discussion started by: salil2012
1 Replies

10. Shell Programming and Scripting

doubt regardin regex in grep

shudnt this command : grep test give all the lines which do not hv 'C'. ^ wrks as negating character inside square brackets right ??? bt in my case grep is printin all the line in the file also wht does grep c+ test & grep c? test shud do ??? (4 Replies)
Discussion started by: evergreen_cool
4 Replies
Login or Register to Ask a Question