Git Product home page Git Product logo

Comments (11)

noxone avatar noxone commented on August 12, 2024

Hi @ableunable ,
thank you for opening the issue.
My Python knowledge is quite limited. So it would be great if you could help me improve the generated code snippet.

It sounds like input is some kind of reserved word or should just not be used here. Which variable name would be better? What about text or maybe inputText?

And I just googled the r-strings a bit more... it seems that there is nearly no escaping needed at all, is it?

So, for the regex \["\], would this code be better?

import re

def useRegex(inputText):
    pattern = re.compile(r"\["'"'r"\]", re.IGNORECASE)
    return pattern.match(inputText)

(I added the quotation mark to include something where an escape sequence is required)

from regex-generator.

5ucur avatar 5ucur commented on August 12, 2024

Good looking out with the quotation mark! Yes, I believe r"\["'"'r"\]" will work. In fact, I checked it right now. The regex compiles and it matches properly. Although, r"\[\"\]" works just as well - actually escaping the quotation mark so as not to close the string, and it looks a bit tidier in my opinion.

I don't know much about regex myself, I just happened to come across this problem while working on some program. But I imagine that anything that needs to be escaped within regex will work the same way as the bracket does.

Regarding input, that's the keyword used to get input from stdin. So you could write a = input("Name: "), and that would output Name: , prompting for input to assign to the variable a. The useRegex function does work with input as the name of the parameter, but it's generally not recommended to use keywords in that way.

On a more aesthetic note, pythonistas prefer 'snake case' over 'camel case'. So inputText and useRegex would become input_text and use_regex.

Hope this was helpful!

from regex-generator.

noxone avatar noxone commented on August 12, 2024

Thank you for the input. I just renamed the variable and method.
And I also just tried different string in this online python interpreter: https://www.programiz.com/python-programming/online-compiler/
The r"\[\"\]" seems to work, yes. It looks a bit easier and tidier, so I will change the code to generate the code you suggested.

from regex-generator.

noxone avatar noxone commented on August 12, 2024

Fixed. Thank you for filing this bug report!

from regex-generator.

5ucur avatar 5ucur commented on August 12, 2024

Sorry if it's not alright to comment on a closed issue, but I just tested it on the site. All works well, except for when I input \. Then the Python code has r""'\\\\' in it, when it could simply have r"\\" (escaping is still necessary for the backslash character itself).

Although I don't know much about regex, from what I see, I think that the Python string should always be exactly the same one as the general regex one, except for escaping backslashes and quotation marks.

from regex-generator.

noxone avatar noxone commented on August 12, 2024

Hi, indeed. This is a case that the code does not yet handle good.
From what I read about the r-strings, they cannot have an odd number of backslashes in the end (because of the escaped quotation mark). So I made my life easy by placing all backslashes at the end of the pattern in an additional (non-r-)string.
You're right: The current behavior is not pretty. I'll try to fix:

  • The empty r-string in the beginning (in case that's the correct behavior)
  • Unnecessary new string for an even number of backslashes

from regex-generator.

noxone avatar noxone commented on August 12, 2024

OK, I published the changes.
Now there is still one bug, I think: The regex \\\\" results in python code r"\\\\\"" which is not correct. So the double quotation marks need to be escaped differently.
So, I think I will use a kind of string for this again...

from regex-generator.

5ucur avatar 5ucur commented on August 12, 2024

If it'll simplify your work, another option would be to not use r-strings and just escape anything that needs to be escaped, in a normal string.
Python sees r"\"" as '\\"' anyway.

from regex-generator.

noxone avatar noxone commented on August 12, 2024

If I understand correctly, the r-strings are made for usage with regex. You don't need to escape anything - except the quotation mark and a backslash in the end.
So I would stick with the r-strings but add a little bit more intelligence.
And well, yes, then I would need to use string concatenation in some cases when single and double quotation marks are used in the same string.

from regex-generator.

noxone avatar noxone commented on August 12, 2024

OK, I hope it's now fixed (once the deployment is done).

from regex-generator.

noxone avatar noxone commented on August 12, 2024

So. for me it looks, like it's working now. If there are " and ' in the same regex it gets a little confusing, but I don't think that can be avoided if you have to escape characters.
Is it OK for you to close this issue?

from regex-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.