Giter VIP home page Giter VIP logo

Comments (7)

Blacksmoke16 avatar Blacksmoke16 commented on August 23, 2024 3

Why would you expect this to return nil? bc is valid based on the regex of [a-zA-Z_]+, and is matched because question_mark_position is making it skip the first character, which otherwise would have matched.

So to me it seems this is working as designed.

from crystal.

Blacksmoke16 avatar Blacksmoke16 commented on August 23, 2024 2

@ksg97031 It's unclear to me what you're wanting to do exactly, but maybe https://crystal-lang.org/api/String.html#match_full%28regex%3ARegex%29%3ARegex%3A%3AMatchData%7CNil-instance-method ?

from crystal.

ksg97031 avatar ksg97031 commented on August 23, 2024 1

@Blacksmoke16 @ysbaddaden
Thank you for the answer.

I was confused because I didn't realize that the keyword 'match' can also refer to a specific part or element that corresponds with another.
So, I substituted the code below to achieve what I wanted using a substring without specifying the position:

input = "a?bc"
match = input[position..].match(/^[a-zA-Z_]+/)
if match
  print(match[0])
end

Could you let me know if there is a way to perform a starts_with operation at a specific position that also returns MatchData?

Thank you!

from crystal.

straight-shoota avatar straight-shoota commented on August 23, 2024 1

String#match with a position argument starts searching for a match from that position. But not limited to that position.
If you want the regex match to be anchored, please use the appropriate option: "a????bc".match(/[a-z]+/, 1, options: :anchored) # => nil

from crystal.

ysbaddaden avatar ysbaddaden commented on August 23, 2024

@ksg97031 You're searching for alphanumeric chars starting from index 1, not exactly at index 1, so the regexp skips the question marks in the string and finds bc. It matched.

from crystal.

ksg97031 avatar ksg97031 commented on August 23, 2024

@Blacksmoke16 I realize my explanation might have been insufficient. Thank you for the advice!

from crystal.

ksg97031 avatar ksg97031 commented on August 23, 2024

String#match with a position argument starts searching for a match from that position. But not limited to that position. If you want the regex match to be anchored, please use the appropriate option: "a????bc".match(/[a-z]+/, 1, options: :anchored) # => nil

Thank you for your assistance!

from crystal.

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.