简体   繁体   English

如何检查我的字符串是否包含连字符,如果包含,如何获取该连字符之前的字符串?

[英]How to check if my string contains a hyphen, and if it does, how to fetch the string before that hyphen?

The string should contain only one hyphen, eg.if the string is ABC-123, then I need to fetch ABC字符串应该只包含一个连字符,例如,如果字符串是 ABC-123,那么我需要获取 ABC

You may match on the following pattern:您可以匹配以下模式:

^[^-]+

This will match the first part of a hyphenated input.这将匹配连字符输入的第一部分。 It also will match the entire input should the input not have any hyphen at all.如果输入根本没有任何连字符,它还将匹配整个输入。

Here is a regex demo .这是一个正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM