LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2020, 07:28 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 443

Rep: Reputation: 12
Howto Find and Replace with RegEx


Hi,
Using Ubuntu 18.04.

Typically in Bluefish Editor, LibreOffice, or Pluma:

I am trying to replace a 10 digit number with a Telephone-Number-like format. (I have a LibreOffice Calc Sheet with about 400 phone no's in 10 digit format)

2025551212 => 201.555.1212

I find the raw number 2025551212 with \d{3}\d{3}\d{4}

Find: \d{3}\d{3}\d{4} or I've tried (\d{3})(\d{3})(\d{4}) or /(\d{3})(\d{3})(\d{4})/

Replace With: ???????????????????????????

to get (\d{3}).(\d{3}).(\d{4})
201 . 555 . 1212

I had an answer to a question similar to this from a LibreOffice Forum and part of the answer I got was:

Quote:
Also you can use Find&Replace whith this options

Find: ([^;]*);(.*) //Robert;Smith
Replace: $2;$1 //Smith;Robert
Regular expresions: ON
Which I have not been able to make work.

Thanks for any help,

R
 
Old 08-16-2020, 09:10 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,347

Rep: Reputation: Disabled
You'll need to use backreferences.

In LibreOffice, you group (parts of) the match for a backreference using non-escaped parentheses:
Code:
(\d{3})(\d{3})(\d{4})
The slightly non-standard part is that you reference these matches with $<number>, rather than the more common \<number>:
Code:
Search for: (\d{3})(\d{3})(\d{4})
Replace with: $1.$2.$3
 
Old 08-16-2020, 09:11 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,161

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Never needed it but a quick test showed this to work for me.
Use the brackets to create capture groups, and $1, $2, $3 to refer to them. So find (\d{3})(\d{3})(\d{4}) and replace
Code:
$1.$2.$3
- I've gotta be the worlds slowest typer ...

Last edited by syg00; 08-16-2020 at 09:13 PM.
 
Old 08-16-2020, 09:44 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
For information: 9195551212
This is random text.
For a good time call Rhoda at 9197820000
More random text.
For time of day ...
3034997111 for WWV (Colorado) or 8083354363 for WWVH (Hawaii).
... this sed ...
Code:
sed -r -e :a -e 's/(.*[0-9])([0-9]{2})([0-9]{3})([0-9]{4})/\1\2-\3-\4/;ta'  \
$InFile >$OutFile
... produced this OutFile ...
Code:
For information: 919-555-1212
This is random text.
For a good time call Rhoda at 919-782-0000
More random text.
For time of day ...
303-499-7111 for WWV (Colorado) or 808-335-4363 for WWVH (Hawaii).
Daniel B. Martin

.
 
Old 08-17-2020, 10:57 AM   #5
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 443

Original Poster
Rep: Reputation: 12
Thanks people,

This only works in LibreOffice which is ok.

SED I don't know about.

R
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find and replace where the find is on two lines and replace on first line andrew777 Linux - General 9 08-05-2013 01:50 PM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 08:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration