You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Java中使用正则表达式提取字符串中的人名信息

Got it, let's work through how to convert your input string to the desired name format using regular expressions. Here's a clear breakdown with practical examples:

核心思路

We need to tackle two key tasks here:

  • Extract all the name components marked with /N
  • Combine those components into a clean name string and prepend the Name: prefix
具体实现方案

1. Regex Pattern to Capture Name Parts

First, use this regex to target and capture each name segment paired with /N:

(\w+)\/N

Let's break it down:

  • (\w+): Captures one or more alphanumeric characters (this grabs each individual name part) — the parentheses create a capture group so we can extract just the name text.
  • \/N: Matches the literal /N marker. We escape the / with a backslash because it's a special character in regex.

2. Python Example (Extract and Combine)

Using Python's re module, we can extract all matching name parts and assemble the final string:

import re

input_str = "I am Duc/N Ta/N Van/N"
# Extract all name parts that end with /N
name_segments = re.findall(r'(\w+)\/N', input_str)
# Combine segments and add the prefix
final_result = f"Name: {' '.join(name_segments)}"

print(final_result)  # Output: Name: Duc Ta Van

3. One-Step Replacement Alternative

If you prefer a single regex replacement to clean up the entire string at once:

import re

input_str = "I am Duc/N Ta/N Van/N"
# Remove the leading "I am " and all /N markers in one pass
clean_name = re.sub(r'^I am |\/N', '', input_str)
# Add the prefix
final_result = f"Name: {clean_name}"

print(final_result)  # Output: Name: Duc Ta Van

The replacement regex here does two things:

  • ^I am : Targets and removes the exact prefix at the start of the string
  • \/N: Removes every instance of the /N marker

4. JavaScript Example

The logic translates directly to other languages too. Here's how you'd do it in JavaScript:

const inputStr = "I am Duc/N Ta/N Van/N";
// Extract all /N-marked parts, strip the marker, then combine
const nameSegments = inputStr.match(/(\w+)\/N/g).map(part => part.replace('/N', ''));
const finalResult = `Name: ${nameSegments.join(' ')}`;

console.log(finalResult); // Output: Name: Duc Ta Van
Quick Note for Edge Cases

If your names might include non-alphanumeric characters (like hyphens or accented letters), swap \w+ with [^\s/]+ — this matches any character except spaces and /, making the regex more flexible for complex names.

内容的提问来源于stack exchange,提问作者AndroidTa

火山引擎 最新活动