如何使用正则表达式裁剪字符串?

时间:2019-01-19 03:34:30

标签: c# regex

这是我想转换的短语。

Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC

我想选这些单词Summoner1, Summoner2, Summoner3, Summoner4。 我想我应该使用正则表达式(regex)来检测字符串"joined the lobby"": ",但是我不知道该怎么做。

谢谢。

有其他疑问的代码。
var list = @"Summoner1 joined the lobby. Summoner2 jonied the lobby. Summoner3: Top Summoner4: ADC"; var result = string.Join("|", list.Select(x => Regex.Replace(x, "( |:).*", string.Empty)));

2 个答案:

答案 0 :(得分:0)

如果要使用正则表达式,可以使用以下内容。

"( |:).*"

示例

var list= @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";


var result = list.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty));

更新:基于评论

var result = string.Join("|",list.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty)));

输出

Summoner1|Summoner2|Summoner3|Summoner4

答案 1 :(得分:0)

<div class="form-group">
                <input type="text" name="name" id="name" class="form-control input-lg"
                       placeholder="experiment name" tabindex="3" required>
            </div>
            <div class="form-group">
                <label for="datetimepicker1"> Start Date </label>
                <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
                    <input type="text" name="start_date" class="form-control datetimepicker-input"
                           data-target="#datetimepicker1"/>
                    <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
                        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="datetimepicker2"> Change Date </label>
                <div class="input-group date" id="datetimepicker2" data-target-input="nearest">
                    <input type="text" name="change_date" class="form-control datetimepicker-input"
                           data-target="#datetimepicker2"/>
                    <div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
                        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="datetimepicker3"> End Date </label>
                <div class="input-group date" id="datetimepicker3" data-target-input="nearest">
                    <input type="text" name="end_date" class="form-control datetimepicker-input"
                           data-target="#datetimepicker3" placeholder="dd/mm/yy"/>
                    <div class="input-group-append" data-target="#datetimepicker3" data-toggle="datetimepicker">
                        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                    </div>
                </div>
            </div>

            <script type="text/javascript">
                $(function () {
                    $('#datetimepicker1').datetimepicker();
                    $('#datetimepicker2').datetimepicker();
                    $('#datetimepicker3').datetimepicker();
                });
            </script>
            <div class="form-group">
                <label for="assets"> Assets </label>
                                <input type="text" name="assets" id="assets" class="form-control input-lg"
                                       placeholder="Assets" tabindex="3" required>
                <select name="assets" class="form-control select_field_class" id="assets">
                    <option value="CO2 SCRUBBER">CO2 SCRUBBER</option>
                    <option value="corn_oil">CORN OIL</option>
                    <option value="DRYERS">DRYERS</option>
                    <option value="ENVIRONMENTAL">ENVIRONMENTAL</option>
                    <option value="UTILITIES">UTILITIES</option>
                    <option value="LAB">LAB</option>
                    <option value="SIEVES">SIEVES</option>
                    <option value="GRAINS & MILLING">GRAINS & MILLING</option>
                    <option value="SEPARATION">SEPARATION</option>
                    <option value="AIR & GAS">AIR & GAS</option>
                    <option value="COOK">COOK</option>
                    <option value="EVAPORATION">EVAPORATION</option>
                    <option value="WATER">WATER</option>
                    <option value="STORAGE">STORAGE</option>
                    <option value="BOILER">BOILER</option>
                    <option value="FERMENTATION">FERMENTATION</option>
                    <option value="BUILDING AND FACILITIES">BUILDING AND FACILITIES</option>
                    <option value="CHEMICAL">CHEMICAL</option>
                </select>
            </div>
            <div class="form-group">
                <label for="assets"> Goals </label>
                <input type="text" name="goals" id="goals" class="form-control input-lg"
                       placeholder="Goals" tabindex="3" required>
            </div>
            <div class="form-group">
                <label for="comments"> Comments </label>
                <textarea name="comments" id="comments" class="form-control input-lg"
                          rows="5" required>
                    </textarea>
            </div>