Hi Howard,
You can use this regex in the Find field:
\d*(?:\((\d+)\))?\d*(?:\((\d+)\))?\d*(?:\((\d+)\))?\d*(?:\((\d+)\))?\d*(?:\((\d+)\))?\d*
with this in the Replace field:
\1 \2 \3 \4 \5
It can find from 1 to 5 parentheses. Repeat the (?:\((\d+)\))?\d* pattern
and the corresponding captures if you need more.
Here is a 2 parentheses case commented:
(?x) (?# allow whitespace and comments)
\d* (?# zero or more leading digits)
(?: (?# non capturing parenthesis)
\( (?# literal open parenthesis)
( (?# capturing open parenthesis - capture \1)
\d+ (?# one or more digits)
) (?# capturing close parenthesis)
\) (?# literal close parenthesis)
)? (?# zero or one occurrence)
\d* (?# zero or more middle digits)
(?: (?# non capturing parenthesis)
\( (?# literal open parenthesis)
( (?# capturing open parenthesis - capture \2)
\d+ (?# one or more digits)
) (?# capturing close parenthesis)
\) (?# literal close parenthesis)
)? (?# zero or one occurrence)
\d* (?# zero or more trailing digits)
You can copy this commented pattern and paste it "as is" in the Find dialog
by right-clicking inside the "Find" field with the <Option-key> pressed and
choosing the "Paste and Select" menu item.
Regards,
Jean Jourdain
On Thursday, June 3, 2021 at 4:46:58 PM UTC+2 Howard wrote:
> Chris, what you wrote is very helpful.
>
> To get the Grep results not enclosed in parentheses, I used the Replace
> pattern `\2` with this Search pattern:
>
> \d*(\((\d+)\))\d*
>
> but that resulted in these numbers:
> 10
> 16
> 10
> 11
>
> Given that the last two numbers, 10 and 11, are on the same line, is there
> a way to modify the Grep expression so that the results appear with the
> final two numbers on the same line, as shown below, with a space separating
> them?
> 10
> 16
> 10 11
>
> Howard
>
>
> On Wednesday, 2 June 2021 at 5:37:42 am UTC-4 [email protected] wrote:
>
>> On 06/01/2021, at 18:59, Howard <[email protected]> wrote:
>>
>> To help me to understand better the Grep part of Neil's solution, can
>> someone provide me with the search pattern and the replace pattern to just
>> find those lines with numbers in parentheses and extract them without
>> any line numbers? I'd like to put that into the Pattern Playground and
>> work with that a bit.
>>
>> ------------------------------
>>
>> Hey Howard,
>>
>> Assuming all of your data lines are variations of this format:
>>
>> 1001405001
>> 0000(10)0000
>> 001320000001
>> 0(16)5000000
>> 021(10)0000(11)
>> 010101000
>>
>> Then it's extremely simple to extract the lines.
>>
>> Find:
>>
>> *.*\(\d+\).**
>>
>> .* == any character zero or more.
>> \( == literal open parenthesis.
>> \d+ == any digit one or more.
>> \) == literal close parenthesis.
>> .* == any character zero or more.
>>
>> ------------------------------
>>
>> If I *knew* that any line containing even 1 parenthesis was viable for
>> extraction I could be lazy and do:
>>
>> Find:
>>
>> *.*\(.**
>>
>> .* == any character zero or more.
>> \( == literal open parenthesis.
>> .* == any character zero or more.
>>
>> OR
>>
>> You could use Neil's suggestion of Process-Lines-Containing with just 1
>> literal parenthesis.
>>
>> ------------------------------
>>
>> Now – to remove the unwanted digits:
>>
>> Find:
>>
>> *\d*(\(\d+\))\d**
>>
>> \d* == any digit zero or more.
>> ( == start capture group.
>> \( == literal parenthesis.
>> \d+ == any digit 1 or more.
>> \) == literal parenthesis.
>> ) == close capture group.
>> \d* == any digit zero or more.
>>
>> Replace:
>>
>> *\1*
>>
>> \1 == capture group 1.
>>
>> ------------------------------
>>
>> As I've mentioned BBEdit is always my starting point for building regular
>> expressions, but there are times when a tool like RegEx101.com will give
>> you more information and more explanation.
>>
>> See your patter here:
>>
>> https://regex101.com/r/eUm1Fo/1
>>
>> --
>> Best Regards,
>> Chris
>>
>>
--
This is the BBEdit Talk public discussion group. If you have a feature request
or need technical support, please email "[email protected]" rather than
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/bbedit/c6bcbe22-5996-4e58-a806-952ddedf84afn%40googlegroups.com.