Comments (7)
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.
@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.
@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.
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.
@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.
@Blacksmoke16 I realize my explanation might have been insufficient. Thank you for the advice!
from crystal.
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)
- MSVC fatal error LNK1103 when using call argument name with null character
- `Compress::Gzip::Header` does not support CRC
- Proposal: `IO::DupReader` HOT 2
- `Digest` should be an `IO` HOT 1
- Proposal: `Pointer::Appender#to_slice`
- [RFC] Allow setting timeouts on `HTTP::Server::Response` HOT 2
- Iterate slice by reference (not by value)
- Third-party library repositories on Windows CI HOT 3
- 1.13.0 - a big problem with UTF strings after `JSON.parse` when some special symbols present; broken encoding HOT 3
- IO::FileDescriptor & Socket finalizers do far too much
- sudo dnf install crystal results in error HOT 13
- Support double splatting on NamedTuple union. HOT 2
- Failed to update crystal with package managers on Windows HOT 8
- Don't require linking to libraries in order to generate documentation HOT 1
- Streamlining the `WaitGroup` API HOT 4
- Fully qualified name must be used inside a block when assigning to an ivar. HOT 1
- A [no overflow check] compiler direective HOT 7
- crystal i crashes in HAR shard HOT 4
- LLVM 18 breaks 128-bit integers in the interpreter HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crystal.