Append a character to a number using Regex

I have some text as follows:

381
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

473
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

112
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

And I need to introduce a special character before each number like so:

±381
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

±473
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

±112
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

So far, I’ve managed to get the regex to replace every number with ±, like so:
$Text = $Text.replace (“[±]?\d+(?:.\d+)?”, “±”);

Which works in TBX. However, the following doesn’t work in TBX, but seems to work in Visual Studio Code:
Find: [±]?\d+(?:.\d+)?
Replace: ±$&

I cannot for the life of me figure out how to execute something like this in TBX.
$Text = $Text.replace (“[±]?\d+(?:.\d+)?”, “±”$&);
How do I make TBX realize that in the replace part of the command, ± is text and $& is regex? Please help. Thanks.

$Text = $Text.replace (“[±]?\d+(?:.\d+)?”, “±”);

I always find it easier to have a narrative of what a regex is doing!

[±]?: An option ± character
\d: at least one digit character, which is not included in the match
(?:.\d+)?: followed optionally by more digits, which are not included in the match

$Text = $Text.replace (“[±]?\d+(?:.\d+)?”, “±”);

OK: this looks for a paragraph that begins with digits, or with a ± followed by digits. If there isn’t already a ±, we insert one.


I wonder whether the streaming parser might be easier here?

Please could you explain how the streaming parser would work?
I think I didn’t explain myself clearly earlier. The character to be appended to each number doesn’t actually matter. I think perhaps using ± caused confusion.

So imagine my $Text is:

381
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

473
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

112
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

And what I want it to be is:
§381
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

§473
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

§112
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

That’s what I’m trying to do. The previous suggestion simply got rid of the entire number and replaced it with the symbol.

Thanks.

Replace the ‘\n’ ‘\n’
with ‘\n’ ‘\n’ ‘€’

You could also refine by replacing ‘\n’ ‘\n’ ‘3 digits’ ‘\n’
with ‘\n’ ‘\n’ ‘€’ ‘3 digits’ ‘\n’

During Saturday’s meetup, we investigates the question and came up with a cleaner take:

var:string s;
$Text.eachLine(x) {
if (x.contains("^\d+")) {
    s=s+"•"+x+"\n";
	} else {
	s=s+x+"\n";
	}
}
$Text=s;

I think this is a little clearer. It would be clearer still if it used some functions:

var:string s;
$Text.eachLine(x) {
if (isNumberedLine(x)) {
    s=processNumberedLine(s,x);
	} else {
    s=processNormalLine(s,x)
    }
}
$Text=s;
4 Likes

A footnote here, and not made or meant in a judgmental way, re use of ‘append’.

Here in the original question ‘append’ is [unintentionally] confusing. The most general understanding is that to append is to ‘add’ and unless stated otherwise, we generally add at the end. However, the task here is to add to the beginning of the matched text. A small yet significant difference, that took several iterations of questioning to clarify.

If facing this same issue of describing a task, ‘prefix’ and ‘suffix’ are more helpful to the reader as they are explicit as to intent. A prefix goes before and a suffix goes after. Thus resolving any ambiguity as to where an appendage occurs in the overall task. By comparison, most—if forced to guess—most would append at the end.

Alternatively, ‘prepend’ already describes adding to the start o something.

My aTbRef inbox regularly reminds me that what I thought to be unambiguous turns out to be anything but … thus my opening comment. :slight_smile:

Thanks for this!

You’re absolutely right. That didn’t quite cross my mind. Thank you.

1 Like