0

I have a pretty complicated regex that i have finally managed to work in JS

  ^\s*((\(\s*([^]+\"|[@#a-zA-Z0-9_-åäöøæüßÅÄÖØÆÜ\-]+){1}(\s{1,}OR\s{1,}(\"[^\"]+\"|[@#a-zA-Z0-9_-åäöøæüßÅÄÖØÆÜ\-]+))*\s*\)))((\s{1,}(AND|NEAR(\/(100|[0-9][0-9]?)(?=\s))?)\s{1,})(\(\s*(\"[^\"]+\"|[@#a-zA-Z0-9_-åäöøæüßÅÄÖØÆÜ\-]+){1}(\s{1,}OR\s{1,}(\"[^\"]+\"|[@#a-zA-Z0-9_-åäöøæüßÅÄÖØÆÜ\-]+))*\s*\)))?(\s{1,}(AND\s{1,}NOT)\s{1,}(\(\s*(\"[^\"]+\"|[@#a-zA-Z0-9_-åäöøæüßÅÄÖØÆÜ\-]+){1}(\s{1,}OR\s{1,}(\"[^\"]+\"|[@#a-zA-Z0-9_-åäöøæüßÅÄÖØÆÜ\-]+))*\s*\)))?(?=(\s*\))?(\;)?\s*$)

with the text: (TITLE: "asdasd")

But when i send the text to my C# backend and uses the same regex it failes. Is there anywhere i can find out why it failes? I have tried some different online tools but none of them explain why it doesn't work.

I just need to be pointed in the right direction here since i'm pretty new working with regex. Thanks!

8
  • If you show us what strings you have, we might be of help simplifying your expression. Commented Dec 18, 2017 at 10:05
  • what do you mean by strings? Do you need more strings than the regex and the test string provided? Commented Dec 18, 2017 at 10:07
  • "the same regex" Are you sure it's the same regex - eg you've got the string escaping correct on the conversion to/from C# from JS - perhaps post exactly how you're using it in both JS and C#. Commented Dec 18, 2017 at 10:20
  • yeah i'm pretty sure of that. I acctually store the regexes in my backend and i call a method in my js to grab them from the backend. Also the js part is working as i want and i guess there is no need to escape the string when i create the regex string and test it in the same class of my c# code? Commented Dec 18, 2017 at 10:23
  • I have tried it in this two: js: regex101.com and .net: regexstorm.net/tester and there i get the same result. the test is working in regex101 and not in regexstorm :( Commented Dec 18, 2017 at 10:25

1 Answer 1

1

The issue is [^] that you use in your expression. In JS syntax, it matches any character including newlines. In .net syntax, this is not a valid regex token.

You should be able to use [\s\S] as a replacement in both, matching any character including newlines.

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

5 Comments

@DanielGustafsson Don't use [\s\S], use . and pass the RegexOptions.Singleline option to the regex compiler. Native ways are always more efficient and more readable.
@WiktorStribiżew I considered that, but afair there is no single-line option for JS regex.
I mean that . and RegexOptons.Singleline should be used in C#.
@Wiktor If I read it correctly OP wants to use the same regex for both. If that is not needed, your comment should be prefered (and the JS regex could remain unchanged)
Yeah i store them in the backend so that i have them in one place. Thanks tho!

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.