0

I already read outer StackOverflow posts and tried the online regex websites.

I want to replace a dot with an underscore:

aab.ccc --> aab_ccc

but in the Matlab code are calculations:

cost11 =aab.ccc*(v1.^3.*t11  + 1.5*v1.*a1.*t11.^2+ a1.^3.*t11./4/4) + aab.ccc.*t11; 

The dots .^ .* ./ should not be replaced. My regex looks like this: \.(?!(\^|\*|\/)$), but it still selects all dots. I also use this regex tool: https://regex101.com/r/rMbYHz/306

What is missing in the regex?

2
  • Do you mean like \.(?![\^*\/\d]) regex101.com/r/qvzkA6/1 also excluding the digits? Commented Apr 23, 2020 at 9:25
  • yes also excluding digits Commented Apr 23, 2020 at 9:30

2 Answers 2

1

For the example data, you could use:

\.(?![*\/\d^])

In parts:

  • \. Match a dot
  • (?! Negative lookahead
    • [*\/\d^] Match either * / a digit or ^
  • ) Close lookahead.

Regex demo

Note that for the pattern (\^|\*|\/)$ you can shorten the alternation using the | to a character class without the pipe. Using $ asserts the end of the string, which can be omitted from the pattern.

Sign up to request clarification or add additional context in comments.

Comments

1

Remove the $

\.(?![\^|\*|\/])

https://regex101.com/r/Qwbcoc/1

2 Comments

but now it also replaces the dot at the digits. I think that better is to add use this one: \.(?!(\^|*|\/|[0-9]))
Well you didn't say that in your question @j35t3r haha, but it seems the fourth bird has thought of that too! Use his answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.