Regex to identify pattern

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Regex to identify pattern
# 1  
Old 10-10-2017
Regex to identify pattern

Hi
In a file I have string in multiple lines. Like below:
Code:
<?=test.getObjectName("L", "testTBL","D") ?> 
<?=test.getObjectName("L", "testTBL","testDB", "D") ?>

I want to use regex to search for the pattern "<?=test.getObjectName...?>"
If the parenthesis has 3 parameters then return 2nd one.
If the parenthesis has 4 parameters then retrun 3rd&2nd.
So expected output:
Code:
testTBL
testDB.testTBL

Thanks in advance
Nitin
# 2  
Old 10-10-2017
Assuming the input is fixed, as you described
Code:
awk -F"[, ]" '
 { O=(NF>6)?$4"."$3:$3; gsub("\"", "", O); print O }
' test.txt

testTBL
testDB.testTBL

# 3  
Old 10-10-2017
Try also
Code:
sed 's/<?=test.getObjectName("[^"]*",//; s/, *"D") *?>//; s/[" ]//g; s/\([^,]*\),\([^,]*\)/\2.\1/ ' file
testTBL
testDB.testTBL

This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-10-2017
Hi Scott
thanks for prompt reply.
However I am specifically looking for regex as the same code may be executed in unix shell or other languages.

Thanks
Nitin
# 5  
Old 10-10-2017
Good then then RudiC just gave you one Smilie It's not pretty, but they often aren't!
# 6  
Old 10-10-2017
Please be aware that a regex does not "return" anything but only matches something. Those matches may be deleted, substituted, shoved around, whatsoever. My proposal above does all of these.
For your problem, you may need two different regexes, one for either case.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex to identify illegal characters in a perso-arabic database

I am working on Sindhi: a perso-Arabic script and since it shares the Unicode-block with over 400 other languages, quite often the database contains characters which are not wanted: illegal characters. I have identified the character set of Sindhi which is given below: For clarity's sake, each... (8 Replies)
Discussion started by: gimley
8 Replies

2. Shell Programming and Scripting

Regex to identify unique words in a dictionary database

Hello, I have a dictionary which I am building for the Open Source Community. The data structure is as under HEADWORD=PARTOFSPEECH=ENGLISH MEANING as shown in the example below अ=m=Prefix signifying negation. अँहँ=ind=Interjection expressing disapprobation. अं=int=An interjection... (2 Replies)
Discussion started by: gimley
2 Replies

3. Shell Programming and Scripting

Identify file pattern, take count of pattern, then act

Guys - Need your ideas on a section of code to finish something up. To make a long story short, I'm parsing a print output file that goes to pre-printed forms. I'm intercepting it, parsing it, formatting it, cutting it up into individual pages, grabbing the text I want in zones, building an... (3 Replies)
Discussion started by: ampsys
3 Replies

4. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

5. Shell Programming and Scripting

Regex to identify word in second position on a line

I am interested in finding a regex to find a word in second position on a line. The word in question is या I tried the following PERL EXPRESSION but it did not work: ] या or ^\W या But both gave Null results I am giving below a Sample file: देना या सौंपना=delegate तह जमना या... (8 Replies)
Discussion started by: gimley
8 Replies

6. Shell Programming and Scripting

Regex to identify a full-stop as a sentence delimiter

Hello, Splitting a sentence using the full-stop/question-mark/exclamation is a common device. Whereas the question-mark / exclamation do not pose too much of a problem; the full-stop as a sentence delimiter raises certain issues because of its varied use: just to name a few. Standard parsers... (9 Replies)
Discussion started by: gimley
9 Replies

7. UNIX for Dummies Questions & Answers

Use Regex to identify / format a complex string

First of all, please have mercy on me. I am not a noob to programming, but I am about as noob as you can get with regex. That being said, I have a problem. I've got a string that looks something like this: Publication - Bob M. Jones, Tony X. Stark, and Fred D. Man, \"Really Awesome Article... (1 Reply)
Discussion started by: egill
1 Replies

8. Shell Programming and Scripting

How to identify the occurence of a pattern between a unique character?

hi, is it possible to find the number of occurences of a pattern between two paranthesis. for e.g i have a file as below. >>{ >>hi >>GoodMorning >>how are you? >>} >>is it good, >>tell me yes, if it is good In the above file, its clear the occurence of word "Good"... (17 Replies)
Discussion started by: divak
17 Replies

9. Shell Programming and Scripting

identify specific pattern

hello I want to create a folder under all partitions that are : /hostname1 /hostname2..... how to exclude /hostname (i.e start directly with /hostname1) thank you (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

10. Shell Programming and Scripting

To identify filename in which having match PATTERN

Hi, Any idea to identify bunch of files( gz format) in which having match PATTERN wanted and print out those files ? :) Regards, (14 Replies)
Discussion started by: cedrichiu
14 Replies
Login or Register to Ask a Question