Revit Macro Regular Expression not capturing pattern "^\d{2}\D{3}\d{4}$" properly

Revit Macro Regular Expression not capturing pattern "^\d{2}\D{3}\d{4}$" properly

paul.t.macknight
Advocate Advocate
1,136 Views
2 Replies
Message 1 of 3

Revit Macro Regular Expression not capturing pattern "^\d{2}\D{3}\d{4}$" properly

paul.t.macknight
Advocate
Advocate

I have a macro, written in C#, that will find all text notes that match the above pattern in the file and update them with a formatted version of the current date (e.g. 06JUN2021). In the off chance that someone else utilizes this format in another location where it is not the only content of the text note, where we would not want to overwrite the whole thing with the date and I am no longer on the project, we need to ensure it only captures text notes that contain exactly this pattern.

 

Before posting, I dug a little more, and found out that the issue was that every text note object inserts the character "\r", or Carriage Return. I have to ask, why is this a functionality of Revit, and why is it not "\r\n" or, more common for coding, "\n" New Line? The New Line character is what most coding languages take and understand, and considering C# lacks "\r" as an end-of-line character and thus does not get recognized by things like Regex patterns ("$", "\Z", and "\z" do not work because the expect a "\n" and do not find one), it makes very little since Revit would maintain use of this character.

 

If anyone could explain to me what the logic is here, I would greatly appreciate it. Otherwise, I intend to make an Ideas post regarding this, as it can have some very annoying and possibly code breaking ramifications for API users.

0 Likes
Accepted solutions (1)
1,137 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

Dear Paul,

 

I love regular expression, use them regularly, and often run into slightly challenging issues with them like the situation you describe.

 

The most effective way to solve this will almost certainly be to add some additional flexibility to your regular expression and deal with the situation as it is.

 

You can enhance your regex to include an optional carriage return or line feed at the beginning of the phrase, repeated any number of times.

 

You statement about C# not supporting the carriage return character is not completely accurate, cf.:

 

https://duckduckgo.com/?q=c%23+carriage+return

 

Line ending conventions vary between operating systems and can be '\n' in Unix, '\r\n' in Windows, and any combinations of '\r' and '\n' in various other situations. C# is designed to (and does) support all of these.

 

I'm afraid I cannot explain the logic behind this. I find that the effective way to approach issues like this in programming is stated by Lord Tennyson. However, I just learned that simply quoting his lines can be considered condescending, so I won't. 🙂

 

https://www.barchaplain.com/?p=13057

 

I hope this helps.

 

Good luck and much success resolving this,

 

Jeremy

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

paul.t.macknight
Advocate
Advocate

Jeremy,

 

I was more curious about Revit's usage of \r when, as you state, Windows OS natively uses \r\n, but I was mistaken about C# not recognizing \r. I attempted incorporating that character into my macro when I discovered Revit's inclusion of the character within text notes, but the results (or rather lack thereof) I got indicated that my regex pattern was not recognized, despite looking at a matchable note in my model. There must have been another issue with my script, though, as when I retried after seeing your comment, it works fine with \r.

 

I appreciate your response, though. Definitely helps clarify what I needed.

0 Likes