Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx - Split Update Statement into back references

Status
Not open for further replies.

spangeman

Programmer
Oct 16, 2004
96
EU
Hi All

I'm just starting to learn regular expressions using perl and am trying to write one to replace update statements in a text file using perl. The following seems to work fine:-

UPDATE (.+) SET (.+) =(.+).WHERE (.+) = (.+)

It allows me the back reference the table and column elements of the statements.

However I am aware from reading several tutorials that .+ is bad form as it is greedy and is open to unitended matches e.g. if another <SPACE>SET appeared in 1 line. The tutorials recommend using more precise mathces rather than using the DOT.

How can I do that in this instance?

Many Thanks
Spangeman
 
Two ways:
[ol]
[li]Use more specific character classes. The any character "." is too general for most situations. Instead use something like \w for word characters or even \S for any non-space characters.[/li]
[li]Use non-greedy matching by adding a ? to your quantifier. Ie "\S+?".[/li]
[/ol]

- Miller
 
not 100% sure but you can try using \s*(\w+)\s* instead of .+ in your statement

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top